Use explicit types on PseudoRandom implementation

This commit is contained in:
sfan5 2024-01-10 16:10:02 +01:00
parent 6f494a968d
commit e83530d40b
2 changed files with 20 additions and 10 deletions

@ -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 @@ public:
}
private:
int m_next;
s32 m_next;
};
class PcgRandom {

@ -38,7 +38,7 @@ public:
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,