![]() |
The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
|
A Pseudorandom Number Generator (PRNG for short). More...
#include <Random.h>
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 LTimer * | m_pTimer = &cTimer |
Pointer to a timer. More... | |
static LRandom * | m_pRandom = &cRandom |
Pointer to a PRNG. More... | |
static LKeyboard * | m_pKeyboard = &cKeyboard |
Pointer to a keyboard handler. More... | |
static LXBoxController * | m_pController = &cController |
Pointer to a XBox controller. More... | |
static LSound * | m_pAudio = &cAudio |
Pointer to an audio player. More... | |
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.
LRandom::LRandom | ( | ) |
Seed the PRNG with an unpredictable value drawn from a timer.
BYTE * LRandom::rand128 | ( | ) |
Generate 128 pseudorandom bits.
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.
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\).
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.
unsigned LRandom::randn | ( | unsigned | i, |
unsigned | j | ||
) |
Generate a pseudorandom unsigned integer within a given range.
i | Bottom of range. |
j | Top of range. |
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.
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.
seed | The seed, defaults to -1. |