Restore original congruential random number generator
Some checks are pending
CodeQL / Analyze (cpp) (push) Waiting to run

This reverts commit 32fee19447
("Fix linear congruential random number generator"),
commit 2252936fc8
("Use linear congruential random number generator from C++11.")
and commit 7b8af67eb5
("[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>
This commit is contained in:
Stefan Weil 2024-11-23 09:20:42 +01:00
parent 66cf74f2dd
commit 5af6cacf84
2 changed files with 14 additions and 9 deletions

View File

@ -27,7 +27,6 @@
#include <cstring>
#include <algorithm> // for std::find
#include <functional>
#include <random>
#include <string>
#include <vector>
@ -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) {
@ -85,7 +85,8 @@ public:
// 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) {
@ -97,10 +98,14 @@ public:
}
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.

View File

@ -92,9 +92,9 @@ protected:
}
// 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
}