From e83530d40b7d065eecdb09300ec0e35da3d2546d Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 10 Jan 2024 16:10:02 +0100 Subject: [PATCH] Use explicit types on PseudoRandom implementation --- src/noise.h | 14 +++++++------- src/unittest/test_random.cpp | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/noise.h b/src/noise.h index ee8efea8306d..6c17afd6885c 100644 --- a/src/noise.h +++ b/src/noise.h @@ -44,23 +44,23 @@ class PseudoRandom { public: const static u32 RANDOM_RANGE = 32767; - inline PseudoRandom(int seed=0): - m_next(seed) + inline PseudoRandom(s32 seed_=0) { + seed(seed_); } - inline void seed(int seed) + inline void seed(s32 seed) { m_next = seed; } - inline int next() + inline u32 next() { m_next = m_next * 1103515245 + 12345; - return (unsigned)(m_next / 65536) % (RANDOM_RANGE + 1); + return (u32)(m_next / 65536) % (RANDOM_RANGE + 1); } - inline int range(int min, int max) + inline s32 range(s32 min, s32 max) { if (max < min) throw PrngException("Invalid range (max < min)"); @@ -77,7 +77,7 @@ class PseudoRandom { } private: - int m_next; + s32 m_next; }; class PcgRandom { diff --git a/src/unittest/test_random.cpp b/src/unittest/test_random.cpp index 14de764e1dbc..9dd88d07e0b2 100644 --- a/src/unittest/test_random.cpp +++ b/src/unittest/test_random.cpp @@ -38,7 +38,7 @@ class TestRandom : public TestBase { void testPcgRandomBytes(); void testPcgRandomNormalDist(); - static const int expected_pseudorandom_results[256]; + static const s32 expected_pseudorandom_results[256]; static const u32 expected_pcgrandom_results[256]; static const u8 expected_pcgrandom_bytes_result[24]; static const u8 expected_pcgrandom_bytes_result2[24]; @@ -63,7 +63,17 @@ void TestRandom::testPseudoRandom() PseudoRandom pr(814538); for (u32 i = 0; i != 256; i++) - UASSERTEQ(int, pr.next(), expected_pseudorandom_results[i]); + UASSERTEQ(s32, pr.next(), expected_pseudorandom_results[i]); + + PseudoRandom pr2(0); + UASSERTEQ(int, pr2.next(), 0); + UASSERTEQ(int, pr2.next(), 21469); + UASSERTEQ(int, pr2.next(), 9989); + + PseudoRandom pr3(-101); + UASSERTEQ(int, pr3.next(), 3267); + UASSERTEQ(int, pr3.next(), 2485); + UASSERTEQ(int, pr3.next(), 30057); } @@ -186,7 +196,7 @@ void TestRandom::testPcgRandomNormalDist() } -const int TestRandom::expected_pseudorandom_results[256] = { +const s32 TestRandom::expected_pseudorandom_results[256] = { 0x02fa, 0x60d5, 0x6c10, 0x606b, 0x098b, 0x5f1e, 0x4f56, 0x3fbd, 0x77af, 0x4fe9, 0x419a, 0x6fe1, 0x177b, 0x6858, 0x36f8, 0x6d83, 0x14fc, 0x2d62, 0x1077, 0x23e2, 0x041b, 0x7a7e, 0x5b52, 0x215d, 0x682b, 0x4716, 0x47e3,