Skip to content

Commit

Permalink
Revert "Use linear congruential random number generator from C++11."
Browse files Browse the repository at this point in the history
This reverts commit 2252936.

Fixes: #4146, #4148, #4270
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Nov 22, 2024
1 parent 5c78037 commit 88de8fe
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/ccutil/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <cstring>
#include <algorithm> // for std::find
#include <functional>
#include <random>
#include <string>
#include <vector>

Expand Down Expand Up @@ -68,12 +67,15 @@ inline const std::vector<std::string> 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) {
Expand All @@ -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) {
Expand All @@ -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.
Expand Down

0 comments on commit 88de8fe

Please sign in to comment.