Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
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.
 
void srand ()
 Seed the random number generator. More...
 
UINT randn ()
 Get a random unsigned integer. More...
 
UINT randn (UINT i, UINT j)
 Get random number in \([i,j]\). More...
 
float randf ()
 Get a random floating point number. More...
 
void randclr (UINT rgb[3])
 Get a random color. More...
 

Private Attributes

UINT m_uState [4] = {0}
 Current state.
 

Detailed Description

A pseudorandom number generator based on xorshift128. It seeds itself with the C standard rand() function, which it assumes has been seeded with something that is different each time that the program is run such as the Windows API function timeGetTime().

Definition at line 38 of file Random.h.

Member Function Documentation

◆ randclr()

void CRandom::randclr ( UINT  rgb[3])

Generate a (hopefully) aesthetically pleasing pseudorandom color by generating a color in HSV format with a random hue, then converting that to RGB format. HSV is used because colors are better distributed in HSV-space than in RGB-space. The colors generated are reasonable dark and highly unlikely to be gray.

Parameters
rgb[out] An aesthetically pleasing pseudorandom RGB color.

Definition at line 97 of file Random.cpp.

◆ randf()

float CRandom::randf ( )

Generate a pseudorandom floating positive point number in \([0,1]\) by generating a pseudorandom unsigned integer and dividing it by \(2^{32} - 1\). Although the result is a float, the internal calculation here is done with doubles to avoid loss of precision (UINT ints have 32 bits but floats have only a 24-bit mantissa). The results are scattered within \([0,1]\) but the randomness is limited by the fact that floats are not randomly distributed in \([0,1]\) to begin with.

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

Definition at line 86 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 this one. Algorithm snarfed from the interwebs.

Returns
A pseudorandom unsigned integer.

Definition at line 49 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 72 of file Random.cpp.

◆ srand()

void CRandom::srand ( )

Seed the pseudorandom number generator by using the linear congruential pseudorandom number generator rand(), which we assume has been seeded from the current time.

Definition at line 39 of file Random.cpp.