Skip to content

Commit

Permalink
Use explicit types on PseudoRandom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jan 14, 2024
1 parent 6f494a9 commit e83530d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/noise.h
Expand Up @@ -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)");
Expand All @@ -77,7 +77,7 @@ class PseudoRandom {
}

private:
int m_next;
s32 m_next;
};

class PcgRandom {
Expand Down
16 changes: 13 additions & 3 deletions src/unittest/test_random.cpp
Expand Up @@ -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];
Expand All @@ -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);
}


Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e83530d

Please sign in to comment.