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 <cstring>
#include <algorithm> // for std::find #include <algorithm> // for std::find
#include <functional> #include <functional>
#include <random>
#include <string> #include <string>
#include <vector> #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. // 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) {
e.seed(seed); 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) {
@ -85,7 +85,8 @@ public:
// 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() {
return e(); Iterate();
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) {
@ -97,10 +98,14 @@ public:
} }
private: private:
std::linear_congruential_engine<std::uint_fast32_t, // Steps the generator to the next value.
6364136223846793005ULL, void Iterate() {
1442695040888963407ULL, seed_ *= 6364136223846793005ULL;
UINT64_MAX> e; 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.

View File

@ -92,9 +92,9 @@ protected:
} }
// Compare sum of all results with expected value. // Compare sum of all results with expected value.
#ifdef FAST_FLOAT #ifdef FAST_FLOAT
EXPECT_FLOAT_EQ(total, 337852.16f); EXPECT_FLOAT_EQ(total, -423236.53f);
#else #else
EXPECT_FLOAT_EQ(total, 337849.39354684710); EXPECT_FLOAT_EQ(total, -423243.392011);
#endif #endif
} }