Lindenmayer System
A Simple L-system Image Generator Featuring Turtle Graphics
Public Member Functions | Private Attributes | List of all members
CRandom Class Reference

Pseudorandom Number Generator (PRNG for short). More...

#include <Random.h>

Public Member Functions

 CRandom ()
 Constructor. More...
 
void srand (int seed=-1)
 Seed the random number generator. More...
 
UINT randn ()
 Get random unsigned integer. More...
 
UINT randn (UINT i, UINT j)
 Get random integer in \([i,j]\). More...
 
float randf ()
 Get random floating point number. More...
 

Private Attributes

UINT m_uState [4]
 Current state.
 

Detailed Description

A simple pseudorandom number generator based on xorshift128. It can be seeded with the time or, if reproducability is desired (eg. when debugging), with a fixed seed.

Definition at line 36 of file Random.h.

Constructor & Destructor Documentation

◆ CRandom()

CRandom::CRandom ( )

Seed the PRNG with an unpredictable value drawn from a timer. Call srand() later to override this.

Definition at line 40 of file Random.cpp.

Member Function Documentation

◆ randf()

float CRandom::randf ( )

Generate a pseudorandom floating positive point number in \([0,1]\)by generatings a pseudorandom unsigned integer and dividing it by \(2^{32} - 1\).

Returns
A pseudorandom floating point number from \([0,1]\).

Definition at line 104 of file Random.cpp.

◆ randn() [1/2]

UINT CRandom::randn ( )

Generate a pseudorandom unsigned integer using xorshift128. This is the one that does the actual work here: The other pseudorandom generation functions rely on it to do the heavy lifting. Algorithm snarfed from the interwebs someplace, I don't quite remember where.

Returns
A pseudorandom unsigned integer.

Definition at line 72 of file Random.cpp.

◆ randn() [2/2]

UINT CRandom::randn ( UINT  i,
UINT  j 
)

Generate a pseudorandom unsigned integer within a range.

Parameters
iBottom of range.
jTop of range.
Returns
A random positive integer r such that i \(\leq\) r \(\leq\) j.

Definition at line 95 of file Random.cpp.

◆ srand()

void CRandom::srand ( int  seed = -1)

If the seed is negative (which it is by default if no parameter is supplied), then use timeGetTime instead (which is, one hopes, unpredictable). The state variables for xorshift128 are initialized using the C Standard Library function rand() seeded using the pro-offered seed value.

Parameters
seedThe seed, defaults to -1.

Definition at line 50 of file Random.cpp.