diff --git a/src/ccutil/helpers.h b/src/ccutil/helpers.h index 212415020b..8df4d5df11 100644 --- a/src/ccutil/helpers.h +++ b/src/ccutil/helpers.h @@ -27,7 +27,6 @@ #include #include // for std::find #include -#include #include #include @@ -68,12 +67,15 @@ inline const std::vector split(const std::string &s, char c) { return v; } -// A simple linear congruential random number generator. +// A simple linear congruential random number generator, using Knuth's +// constants from: +// http://en.wikipedia.org/wiki/Linear_congruential_generator. class TRand { public: + TRand() = default; // Sets the seed to the given value. void set_seed(uint64_t seed) { - e.seed(seed); + seed_ = seed; } // Sets the seed using a hash of a string. void set_seed(const std::string &str) { @@ -83,7 +85,8 @@ class TRand { // Returns an integer in the range 0 to INT32_MAX. int32_t IntRand() { - return e(); + Iterate(); + return seed_ >> 33; } // Returns a floating point value in the range [-range, range]. double SignedRand(double range) { @@ -95,7 +98,14 @@ class TRand { } private: - std::minstd_rand e; + // Steps the generator to the next value. + void Iterate() { + seed_ *= 6364136223846793005ULL; + seed_ += 1442695040888963407ULL; + } + + // The current value of the seed. + uint64_t seed_{1}; }; // Remove newline (if any) at the end of the string.