The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
Public Member Functions | Private Attributes | List of all members
LRandom Class Reference

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

#include <Random.h>

Inheritance diagram for LRandom:
LComponent

Public Member Functions

 LRandom ()
 Constructor. More...
 
void srand (int seed=-1)
 Seed the random number generator. More...
 
unsigned randn ()
 Get a random unsigned integer. More...
 
unsigned randn (unsigned i, unsigned j)
 Get random integer in \([i,j]\). More...
 
float randf ()
 Get a random floating point number. More...
 
Vector2 randv ()
 Get a random direction. More...
 
XMFLOAT4 randclr ()
 Get a random color. More...
 
BYTE * rand128 ()
 Get 128 random bits. More...
 

Private Attributes

unsigned m_uState [4]
 Current state.
 

Additional Inherited Members

- Static Protected Attributes inherited from LComponent
static LTimerm_pTimer = &cTimer
 Pointer to a timer. More...
 
static LRandomm_pRandom = &cRandom
 Pointer to a PRNG. More...
 
static LKeyboardm_pKeyboard = &cKeyboard
 Pointer to a keyboard handler. More...
 
static LXBoxControllerm_pController = &cController
 Pointer to a XBox controller. More...
 
static LSoundm_pAudio = &cAudio
 Pointer to an audio player. More...
 

Detailed Description

LRandom is 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. It has functions for generating pseudorandom unsigned integers, floats, unit-length vectors, and colors.

Constructor & Destructor Documentation

◆ LRandom()

LRandom::LRandom ( )

Seed the PRNG with an unpredictable value drawn from a timer.

Member Function Documentation

◆ rand128()

BYTE * LRandom::rand128 ( )

Generate 128 pseudorandom bits.

Returns
A pointer to an array of 16 pseudo-random bytes (128 bits)

◆ randclr()

XMFLOAT4 LRandom::randclr ( )

Generate an aesthetically pleasing pseudorandom color by generating a color in HSV format with random hue, saturation 0.5, and value 1.0 and then converting that to RGB. HSV is used because colors are better distributed in HSV-space than in RGB-space. There will probably be a lot of repeated colors generated by this function but probably not consecutively. The colors generated will be light and bright highly unlikely to be gray. The alpha value is set to 1, that is, there is no transparency.

Returns
A hopefully aesthetically pleasing pseudorandom color.

◆ randf()

float LRandom::randf ( )

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

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

◆ randn() [1/2]

unsigned LRandom::randn ( )

Generate a pseudorandom unsigned integer using xorshift128. This is the one that does the actual work here: The other psuedorandom generation functions rely on this one. Algorithm snarfed from the interwebs.

Returns
A pseudorandom unsigned integer.

◆ randn() [2/2]

unsigned LRandom::randn ( unsigned  i,
unsigned  j 
)

Generate a pseudorandom unsigned integer within a given range.

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

◆ randv()

Vector2 LRandom::randv ( )

Generate a pseudorandom unit vector by generating an angle in the range \([0, 2\pi]\) and returning the unit vector with that orientation.

Returns
A pseudorandom unit vector.

◆ srand()

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

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

Parameters
seedThe seed, defaults to -1.