-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,19 +24,17 @@ | |
#include <cstdio> | ||
#include <cstring> | ||
#include <functional> | ||
#include <random> | ||
#include <string> | ||
|
||
namespace tesseract { | ||
|
||
// A simple linear congruential random number generator, using Knuth's | ||
// constants from: | ||
// http://en.wikipedia.org/wiki/Linear_congruential_generator. | ||
// A simple linear congruential random number generator. | ||
class TRand { | ||
public: | ||
TRand() = default; | ||
// Sets the seed to the given value. | ||
void set_seed(uint64_t seed) { | ||
seed_ = seed; | ||
e.seed(seed); | ||
} | ||
// Sets the seed using a hash of a string. | ||
void set_seed(const std::string& str) { | ||
|
@@ -46,8 +44,7 @@ class TRand { | |
|
||
// Returns an integer in the range 0 to INT32_MAX. | ||
int32_t IntRand() { | ||
Iterate(); | ||
return seed_ >> 33; | ||
return e(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
egorpugin
Author
Contributor
|
||
} | ||
// Returns a floating point value in the range [-range, range]. | ||
double SignedRand(double range) { | ||
|
@@ -59,14 +56,7 @@ class TRand { | |
} | ||
|
||
private: | ||
// Steps the generator to the next value. | ||
void Iterate() { | ||
seed_ *= 6364136223846793005ULL; | ||
seed_ += 1442695040888963407ULL; | ||
} | ||
|
||
// The current value of the seed. | ||
uint64_t seed_{1}; | ||
std::minstd_rand e; | ||
}; | ||
|
||
// Remove newline (if any) at the end of the string. | ||
|
3 comments
on commit 2252936
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reproducible results are very important for my work. Changing to a different random generator also changes all processes which use that generator. What do we gain by using the new code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can revert if you'd like to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should match previous behavior.
We can add a test with original impl and such std test to compare some values if you'd like to keep the same RNG.
int32_t IntRand() {
return e() >> 33;
}
private:
std::linear_congruential_engine<std::uint64_t, 6364136223846793005ULL, 1442695040888963407ULL, 1ULL << 63> e;
Are you sure that this still returns an integer in the range 0 to INT32_MAX?