SAGE
A Simple Academic Game Engine
Loading...
Searching...
No Matches
Sage::CRandom Class Reference

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

#include <SageRandom.h>

Inheritance diagram for Sage::CRandom:
Collaboration diagram for Sage::CRandom:

Public Member Functions

 CRandom ()
 Constructor.
 
void srand (UINT)
 Seed the random number generator.
 
void srand ()
 Seed the random number generator.
 
UINT randn ()
 Get a random unsigned integer.
 
UINT randn (UINT, UINT)
 Get random integer in \([i,j]\).
 
float randf ()
 Get a random floating point number.
 
Vector2 randv ()
 Get a random 2D direction.
 
Vector3 randv3 ()
 Get a random 3D direction.
 
XMFLOAT4 randclr ()
 Get a random color.
 

Private Attributes

std::default_random_engine m_stdRandom
 Pseudo-random number generator.
 

Additional Inherited Members

- Static Protected Attributes inherited from Sage::CComponent
static std::unique_ptr< CTimerm_pTimer = std::make_unique<CTimer>()
 Pointer to a timer.
 
static std::unique_ptr< CRandomm_pRandom = std::make_unique<CRandom>()
 Pointer to a PRNG.
 
static std::unique_ptr< CSoundm_pSound = std::make_unique<CSound>()
 Pointer to sound manager.
 
static std::unique_ptr< CKeyboardm_pKeyboard
 Pointer to a keyboard handler.
 
static std::unique_ptr< CMousem_pMouse = std::make_unique<CMouse>()
 Pointer to a mouse handler.
 
static std::unique_ptr< CControllerm_pController
 Pointer to a controller.
 

Detailed Description

CRandom is a simple pseudo-random number generator. It can be seeded with the time or, if reproducability is desired (eg. when debugging), with a fixed seed. It has functions for generating pseudo-random unsigned integers, floats, unit vectors, and colors.

Constructor & Destructor Documentation

◆ CRandom()

CRandom::CRandom ( )

The constructor seeds the pseudo-random number generator, just in case the programmer forgets to do it.

Here is the call graph for this function:

Member Function Documentation

◆ randclr()

XMFLOAT4 CRandom::randclr ( )

Generate an aesthetically pleasing pseudo-random 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 channel is set to 1, that is, there is no transparency.

Returns
A hopefully aesthetically pleasing pseudo-random color.
Here is the call graph for this function:

◆ randf()

float CRandom::randf ( )

Generate a pseudo-random floating positive point number in \([0,1]\).

Returns
A pseudo-random floating point number from \([0,1]\).
Here is the caller graph for this function:

◆ randn() [1/2]

UINT CRandom::randn ( )

Generate a pseudo-random unsigned integer.

Returns
A pseudo-random unsigned integer.

◆ randn() [2/2]

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

Generate a pseudo-random unsigned integer within a given range.

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

◆ randv()

Vector2 CRandom::randv ( )

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

Returns
A pseudo-random unit 2D vector.
Here is the call graph for this function:

◆ randv3()

Vector3 CRandom::randv3 ( )

Generate a pseudo-random unit 3D vector from a uniform spherical distribution. Algorithm from http://stackoverflow.com/questions/5408276/python-uniform-spherical-distribution

Returns
A pseudo-random unit 3D vector.
Here is the call graph for this function:

◆ srand() [1/2]

void CRandom::srand ( )

Seed the pseudo-random number generator with the Unix epoch time (number of seconds since Jan 1, 1970 UTC, excluding leap seconds).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ srand() [2/2]

void CRandom::srand ( UINT n)

Seed the pseudo-random number generator.

Parameters
nThe seed value.