File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed
Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change 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.
7273class TRand {
7374public:
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
97100private:
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.
You can’t perform that action at this time.
0 commit comments