Skip to content

Commit 88de8fe

Browse files
committed
Revert "Use linear congruential random number generator from C++11."
This reverts commit 2252936. Fixes: #4146, #4148, #4270 Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 5c78037 commit 88de8fe

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/ccutil/helpers.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <cstring>
2828
#include <algorithm> // for std::find
2929
#include <functional>
30-
#include <random>
3130
#include <string>
3231
#include <vector>
3332

@@ -68,12 +67,15 @@ inline const std::vector<std::string> split(const std::string &s, char c) {
6867
return v;
6968
}
7069

71-
// A simple linear congruential random number generator.
70+
// A simple linear congruential random number generator, using Knuth's
71+
// constants from:
72+
// http://en.wikipedia.org/wiki/Linear_congruential_generator.
7273
class TRand {
7374
public:
75+
TRand() = default;
7476
// Sets the seed to the given value.
7577
void set_seed(uint64_t seed) {
76-
e.seed(seed);
78+
seed_ = seed;
7779
}
7880
// Sets the seed using a hash of a string.
7981
void set_seed(const std::string &str) {
@@ -83,7 +85,8 @@ class TRand {
8385

8486
// Returns an integer in the range 0 to INT32_MAX.
8587
int32_t IntRand() {
86-
return e();
88+
Iterate();
89+
return seed_ >> 33;
8790
}
8891
// Returns a floating point value in the range [-range, range].
8992
double SignedRand(double range) {
@@ -95,7 +98,14 @@ class TRand {
9598
}
9699

97100
private:
98-
std::minstd_rand e;
101+
// Steps the generator to the next value.
102+
void Iterate() {
103+
seed_ *= 6364136223846793005ULL;
104+
seed_ += 1442695040888963407ULL;
105+
}
106+
107+
// The current value of the seed.
108+
uint64_t seed_{1};
99109
};
100110

101111
// Remove newline (if any) at the end of the string.

0 commit comments

Comments
 (0)