Skip to content

Commit 5af6cac

Browse files
committed
Restore original congruential random number generator
This reverts commit 32fee19 ("Fix linear congruential random number generator"), commit 2252936 ("Use linear congruential random number generator from C++11.") and commit 7b8af67 ("[test] Fix intsimdmatrix test. Update result value based on updated TRand engine."). It restores the original congruential random number generator and the related unittest. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 66cf74f commit 5af6cac

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/ccutil/helpers.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <cstring>
2828
#include <algorithm> // for std::find
2929
#include <functional>
30-
#include <random>
3130
#include <string>
3231
#include <vector>
3332

@@ -73,9 +72,10 @@ inline const std::vector<std::string> split(const std::string &s, char c) {
7372
// http://en.wikipedia.org/wiki/Linear_congruential_generator.
7473
class TRand {
7574
public:
75+
TRand() = default;
7676
// Sets the seed to the given value.
7777
void set_seed(uint64_t seed) {
78-
e.seed(seed);
78+
seed_ = seed;
7979
}
8080
// Sets the seed using a hash of a string.
8181
void set_seed(const std::string &str) {
@@ -85,7 +85,8 @@ class TRand {
8585

8686
// Returns an integer in the range 0 to INT32_MAX.
8787
int32_t IntRand() {
88-
return e();
88+
Iterate();
89+
return seed_ >> 33;
8990
}
9091
// Returns a floating point value in the range [-range, range].
9192
double SignedRand(double range) {
@@ -97,10 +98,14 @@ class TRand {
9798
}
9899

99100
private:
100-
std::linear_congruential_engine<std::uint_fast32_t,
101-
6364136223846793005ULL,
102-
1442695040888963407ULL,
103-
UINT64_MAX> 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};
104109
};
105110

106111
// Remove newline (if any) at the end of the string.

unittest/intsimdmatrix_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ class IntSimdMatrixTest : public ::testing::Test {
9292
}
9393
// Compare sum of all results with expected value.
9494
#ifdef FAST_FLOAT
95-
EXPECT_FLOAT_EQ(total, 337852.16f);
95+
EXPECT_FLOAT_EQ(total, -423236.53f);
9696
#else
97-
EXPECT_FLOAT_EQ(total, 337849.39354684710);
97+
EXPECT_FLOAT_EQ(total, -423243.392011);
9898
#endif
9999
}
100100

0 commit comments

Comments
 (0)