Skip to content

Commit

Permalink
Restore original congruential random number generator
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stweil committed Nov 23, 2024
1 parent 66cf74f commit 5af6cac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 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 @@ -73,9 +72,10 @@ inline const std::vector<std::string> split(const std::string &s, char c) {
// 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 @@ -85,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 @@ -97,10 +98,14 @@ class TRand {
}

private:
std::linear_congruential_engine<std::uint_fast32_t,
6364136223846793005ULL,
1442695040888963407ULL,
UINT64_MAX> 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
4 changes: 2 additions & 2 deletions unittest/intsimdmatrix_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class IntSimdMatrixTest : public ::testing::Test {
}
// Compare sum of all results with expected value.
#ifdef FAST_FLOAT
EXPECT_FLOAT_EQ(total, 337852.16f);
EXPECT_FLOAT_EQ(total, -423236.53f);
#else
EXPECT_FLOAT_EQ(total, 337849.39354684710);
EXPECT_FLOAT_EQ(total, -423243.392011);
#endif
}

Expand Down

0 comments on commit 5af6cac

Please sign in to comment.