Use linear congruential random number generator from C++11.

This commit is contained in:
Egor Pugin 2020-12-31 02:31:53 +03:00
parent a0509b2feb
commit 2252936fc8

View File

@ -24,19 +24,17 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <functional> #include <functional>
#include <random>
#include <string> #include <string>
namespace tesseract { namespace tesseract {
// A simple linear congruential random number generator, using Knuth's // A simple linear congruential random number generator.
// constants from:
// http://en.wikipedia.org/wiki/Linear_congruential_generator.
class TRand { class TRand {
public: public:
TRand() = default;
// Sets the seed to the given value. // Sets the seed to the given value.
void set_seed(uint64_t seed) { void set_seed(uint64_t seed) {
seed_ = seed; e.seed(seed);
} }
// Sets the seed using a hash of a string. // Sets the seed using a hash of a string.
void set_seed(const std::string& str) { void set_seed(const std::string& str) {
@ -46,8 +44,7 @@ class TRand {
// Returns an integer in the range 0 to INT32_MAX. // Returns an integer in the range 0 to INT32_MAX.
int32_t IntRand() { int32_t IntRand() {
Iterate(); return e();
return seed_ >> 33;
} }
// Returns a floating point value in the range [-range, range]. // Returns a floating point value in the range [-range, range].
double SignedRand(double range) { double SignedRand(double range) {
@ -59,14 +56,7 @@ class TRand {
} }
private: private:
// Steps the generator to the next value. std::minstd_rand e;
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. // Remove newline (if any) at the end of the string.