CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
cugl::Random Class Reference

#include <CURandom.h>

Public Member Functions

 Random ()
 
 ~Random ()
 
void dispose ()
 
bool init ()
 
bool initWithSeed (Uint64 seed)
 
bool initWithArray (Uint64 *key, size_t len)
 
void reset ()
 
void reset (Uint64 seed)
 
void reset (Uint64 *key, size_t len)
 
Uint64 getSeed () const
 
const std::vector< Uint64 > & getKeys () const
 
Uint64 getUint64 ()
 
Sint64 getSint64 ()
 
Uint64 getOpenUint64 (Uint64 min, Uint64 max)
 
Uint64 getClosedUint64 (Uint64 min, Uint64 max)
 
Sint64 getOpenSint64 (Sint64 min, Sint64 max)
 
Sint64 getClosedSint64 (Sint64 min, Sint64 max)
 
Uint32 getUint32 ()
 
Sint32 getSint32 ()
 
Uint16 getUint16 ()
 
Sint16 getSint16 ()
 
Uint8 getUint8 ()
 
Sint8 getSint8 ()
 
bool getBool ()
 
double getDouble ()
 
double getClosedDouble ()
 
double getHalfOpenDouble ()
 
double getOpenDouble ()
 
double getClosedDouble (double min, double max)
 
double getOpenDouble (double min, double max)
 
double getRightOpenDouble (double min, double max)
 
double getLeftOpenDouble (double min, double max)
 
double getFloat ()
 
float getClosedFloat (float min, float max)
 
float getOpenFloat (float min, float max)
 
float getRightOpenFloat (float min, float max)
 
float getLeftOpenFloat (float min, float max)
 
template<typename T >
TgetChoice (T *data, size_t len)
 
template<typename T >
TgetChoice (const std::vector< T > &data)
 
template<typename T >
void shuffle (T *data, size_t len)
 
template<typename T >
void shuffle (std::vector< T > &data)
 
double getNormal (double mu, double sigma)
 
double getLogNorm (double mu, double sigma)
 
double getExp (double mu)
 
double getGamma (double alpha, double beta)
 
double getBeta (double alpha, double beta)
 
double getPareto (double xm, double alpha)
 
double getWeibull (double k, double lambda)
 

Static Public Member Functions

static std::shared_ptr< Randomalloc ()
 
static std::shared_ptr< RandomallocWithSeed (Uint64 seed)
 
static std::shared_ptr< RandomallocWithArray (Uint64 *key, size_t len)
 

Detailed Description

This class is a 64 bit Marseene Twister psuedo-random generator.

This generator is guaranteed to be cross-platform with respect to random integers. So, given the same seed, any two different platforms will generate the same sequence of random integers.

For the case of reals (e.g. doubles), cross-platform support depends on IEEE 754, which is supported by all modern hardware. Any two platforms that support IEEE 754 should generate the same numbers for the same hardware.

However, there are no cross-platform guarantees for any of the distribution functions like getNormal or getGamma. This distributions use complex mathematical functions that may be implemented differently on different platforms.

Constructor & Destructor Documentation

◆ Random()

cugl::Random::Random ( )
inline

Creates an uninitialized generator with no sequence.

You must initialize the generator to create a pseudo-random sequence.

◆ ~Random()

cugl::Random::~Random ( )
inline

Deletes this generator, disposing all resources.

Member Function Documentation

◆ alloc()

static std::shared_ptr< Random > cugl::Random::alloc ( )
inlinestatic

Returns a newly allocated psuedorandom number generator with the given time

The random number generator is the classic 64 bit version implement here

http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
Returns
a newly allocated psuedorandom number generator with the given time

◆ allocWithArray()

static std::shared_ptr< Random > cugl::Random::allocWithArray ( Uint64 *  key,
size_t  len 
)
inlinestatic

Returns a newly allocated psuedorandom number generator with the given keys

The random number generator is the classic 64 bit version implement here

http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c

Generators with the same key sequence will generate the same numbers.

Parameters
keyThe array of generator keys
lenThe key length
Returns
a newly allocated psuedorandom number generator with the given keys

◆ allocWithSeed()

static std::shared_ptr< Random > cugl::Random::allocWithSeed ( Uint64  seed)
inlinestatic

Returns a newly allocated psuedorandom number generator with the given seed

The random number generator is the classic 64 bit version implement here

http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c

Generators with the same seed will generate the same numbers.

Parameters
seedThe generator seed
Returns
a newly allocated psuedorandom number generator with the given seed

◆ dispose()

void cugl::Random::dispose ( )

Disposes the psuedo-random sequence for this generator

You must reinitialize the generator to use it.

◆ getBeta()

double cugl::Random::getBeta ( double  alpha,
double  beta 
)
inline

Returns the next element in the beta distribution.

The parameters alpha and beta should be positive. The values returned are between 0 and 1.

The mean is alpha/(alpha+beta) and the variance is (alpha*beta)/((alpha+beta+1)*(alpha+beta)^2)

Parameters
alphaThe first shape parameter (should be > 0)
betaThe second shape parameter (should be > 0)
Returns
the next element in the beta distribution.

◆ getBool()

bool cugl::Random::getBool ( )
inline

Returns the next pseudorandom value true or false.

Returns
the next pseudorandom value true or false.

◆ getChoice() [1/2]

template<typename T >
T * cugl::Random::getChoice ( const std::vector< T > &  data)
inline

Returns a pointer to a randomly selected item in data

The function works on a vector. If the vector is empty, this function returns nullptr.

Parameters
dataThe vector to select from
Returns
a pointer to a randomly selected item in data

◆ getChoice() [2/2]

template<typename T >
T * cugl::Random::getChoice ( T data,
size_t  len 
)
inline

Returns a pointer to a randomly selected item in data

The function works on an array of data. The length must be specified. If len == 0, this function returns nullptr.

Parameters
dataThe array to select from
lenThe number of elements in the array
Returns
a pointer to a randomly selected item in data

◆ getClosedDouble() [1/2]

double cugl::Random::getClosedDouble ( )
inline

Returns the next pseudorandom double in [0,1]

Both endpoints of the interval are included.

Returns
the next pseudorandom double in [0,1]

◆ getClosedDouble() [2/2]

double cugl::Random::getClosedDouble ( double  min,
double  max 
)
inline

Returns the next pseudorandom double in [min,max]

Both endpoints of the interval are included. If min > max, the result is undefined.

Returns
the next pseudorandom double in [min,max]

◆ getClosedFloat()

float cugl::Random::getClosedFloat ( float  min,
float  max 
)
inline

Returns the next pseudorandom float in [min,max]

Both endpoints of the interval are included. If min > max, the result is undefined.

Returns
the next pseudorandom float in [min,max]

◆ getClosedSint64()

Sint64 cugl::Random::getClosedSint64 ( Sint64  min,
Sint64  max 
)
inline

Returns the next pseudorandom signed integer in [min, max]

If min >= max, the result is undefined.

Returns
the next pseudorandom signed integer in [min, max]

◆ getClosedUint64()

Uint64 cugl::Random::getClosedUint64 ( Uint64  min,
Uint64  max 
)
inline

Returns the next pseudorandom integer in [min, max]

If min >= max, the result is undefined.

Returns
the next pseudorandom integer in [min, max]

◆ getDouble()

double cugl::Random::getDouble ( )
inline

Returns the next pseudorandom double in [0,1)

Only the endpoint 0 is included. To get a random double in the interval (0,1], simply subtract this number from 1.

This function is equivalent to getHalfOpenDouble, as that is often the desired behavior of random generators.

Returns
the next pseudorandom double in [0,1)

◆ getExp()

double cugl::Random::getExp ( double  mu)
inline

Returns the next element in the exponential distribution.

The value mu is the desired mean. It should be nonzero. Returned values range from 0 to positive infinity if mu is positive, and from negative infinity to 0 if mu is negative.

Parameters
muThe standard deviation
Returns
the next element in the exponential distribution.

◆ getFloat()

double cugl::Random::getFloat ( )
inline

Returns the next pseudorandom float in [0,1)

Only the endpoint 0 is included. To get a random double in the interval (0,1], simply subtract this number from 1.

Returns
the next pseudorandom float in [0,1)

◆ getGamma()

double cugl::Random::getGamma ( double  alpha,
double  beta 
)
inline

Returns the next element in the gamma distribution.

The parameters alpha and beta should be positive. The probability distribution function is

             x^(alpha - 1) * exp(-x * beta) * beta^alpha
 pdf(x) =  -----------------------------------------------
                              gamma(alpha)

where gamma() is the gamma function. See

https://en.wikipedia.org/wiki/Gamma_distribution

The mean is is alpha/beta, and the variance is alpha/(beta^2).

Parameters
alphaThe shape parameter (should be > 0)
betaThe rate parameter (should be > 0)
Returns
the next element in the gamma distribution.

◆ getHalfOpenDouble()

double cugl::Random::getHalfOpenDouble ( )
inline

Returns the next pseudorandom double in [0,1)

Only the endpoint 0 is included. To get a random double in the interval (0,1], simply subtract this number from 1.

Returns
the next pseudorandom double in [0,1)

◆ getKeys()

const std::vector< Uint64 > & cugl::Random::getKeys ( ) const
inline

Returns the key sequence for the given generator.

If a seed was used in place of a key sequence, this method will return an empty vector.

Returns
the seed for the given generator.

◆ getLeftOpenDouble()

double cugl::Random::getLeftOpenDouble ( double  min,
double  max 
)
inline

Returns the next pseudorandom double in (min,max]

Only the second endpoint of the interval is included. If min >= max, the result is undefined.

Returns
the next pseudorandom double in (min,max]

◆ getLeftOpenFloat()

float cugl::Random::getLeftOpenFloat ( float  min,
float  max 
)
inline

Returns the next pseudorandom float in (min,max]

Only the second endpoint of the interval is included. If min >= max, the result is undefined.

Returns
the next pseudorandom float in (min,max]

◆ getLogNorm()

double cugl::Random::getLogNorm ( double  mu,
double  sigma 
)
inline

Returns the next element in the log normal distribution.

If you take the natural logarithm of this distribution, you will get a normal distribution with mean mu and standard deviation sigma. Parameter mu can have any value, and sigma must be greater than zero.

Parameters
muThe mean
sigmaThe standard deviation
Returns
the next element in the log normal distribution.

◆ getNormal()

double cugl::Random::getNormal ( double  mu,
double  sigma 
)
inline

Returns the next element in the normal distribution.

The value mu is the mean, and sigma is the standard deviation. Parameter mu can have any value, and sigma must be greater than zero.

Parameters
muThe mean
sigmaThe standard deviation
Returns
the next element in the normal distribution.

◆ getOpenDouble() [1/2]

double cugl::Random::getOpenDouble ( )
inline

Returns the next pseudorandom double in (0,1)

Neither endpoint in the interval is included.

Returns
the next pseudorandom double in (0,1)

◆ getOpenDouble() [2/2]

double cugl::Random::getOpenDouble ( double  min,
double  max 
)
inline

Returns the next pseudorandom double in (min,max)

Neither of the endpoints of the interval are included. If min >= max, the result is undefined.

Returns
the next pseudorandom double in (min,max)

◆ getOpenFloat()

float cugl::Random::getOpenFloat ( float  min,
float  max 
)
inline

Returns the next pseudorandom float in (min,max)

Neither of the endpoints of the interval are included. If min >= max, the result is undefined.

Returns
the next pseudorandom float in (min,max)

◆ getOpenSint64()

Sint64 cugl::Random::getOpenSint64 ( Sint64  min,
Sint64  max 
)
inline

Returns the next pseudorandom signed integer in [min, max)

If min >= max, the result is undefined.

Returns
the next pseudorandom signed integer in [min, max)

◆ getOpenUint64()

Uint64 cugl::Random::getOpenUint64 ( Uint64  min,
Uint64  max 
)
inline

Returns the next pseudorandom integer in [min, max)

If min >= max, the result is undefined.

Returns
the next pseudorandom integer in [min, max)

◆ getPareto()

double cugl::Random::getPareto ( double  xm,
double  alpha 
)
inline

Returns the next element in the Pareto distribution.

The mean is infty for alpha <= 1 and (alpha*xm)/(alpha-1) for alpha > 1. The variance is infty for alpha <= 2 and (alpha*xm^2)/((alpha-2)*(alpha-1)^2) for alpha > 2.

Parameters
xmThe scale parameter
alphaThe shape parameter
Returns
the next element in the Pareto distribution.

◆ getRightOpenDouble()

double cugl::Random::getRightOpenDouble ( double  min,
double  max 
)
inline

Returns the next pseudorandom double in [min,max)

Only the first endpoint of the interval is included. If min >= max, the result is undefined.

Returns
the next pseudorandom double in [min,max)

◆ getRightOpenFloat()

float cugl::Random::getRightOpenFloat ( float  min,
float  max 
)
inline

Returns the next pseudorandom float in [min,max)

Only the first endpoint of the interval is included. If min >= max, the result is undefined.

Returns
the next pseudorandom float in [min,max)

◆ getSeed()

Uint64 cugl::Random::getSeed ( ) const
inline

Returns the seed for the given generator.

If a key sequence was used in place of a seed, this method will return 0.

Returns
the seed for the given generator.

◆ getSint16()

Sint16 cugl::Random::getSint16 ( )
inline

Returns the next pseudorandom integer in [-2^15, 2^15-1]

Returns
the next pseudorandom integer in [-2^15, 2^15-1]

◆ getSint32()

Sint32 cugl::Random::getSint32 ( )
inline

Returns the next pseudorandom integer in [-2^31, 2^31-1]

Returns
the next pseudorandom integer in [-2^31, 2^31-1]

◆ getSint64()

Sint64 cugl::Random::getSint64 ( )
inline

Returns the next pseudorandom integer in [-2^63, 2^63-1]

Returns
the next pseudorandom integer in [-2^63, 2^63-1]

◆ getSint8()

Sint8 cugl::Random::getSint8 ( )
inline

Returns the next pseudorandom integer in [-128,127]

Returns
the next pseudorandom integer in [-128,127]

◆ getUint16()

Uint16 cugl::Random::getUint16 ( )
inline

Returns the next pseudorandom integer in [0, 2^16-1]

Returns
the next pseudorandom integer in [0, 2^16-1]

◆ getUint32()

Uint32 cugl::Random::getUint32 ( )
inline

Returns the next pseudorandom integer in [0, 2^32-1]

Returns
the next pseudorandom integer in [0, 2^32-1]

◆ getUint64()

Uint64 cugl::Random::getUint64 ( )
inline

Returns the next pseudorandom integer in [0, 2^64-1]

Returns
the next pseudorandom integer in [0, 2^64-1]

◆ getUint8()

Uint8 cugl::Random::getUint8 ( )
inline

Returns the next pseudorandom integer in [0, 255]

Returns
the next pseudorandom integer in [0, 255]

◆ getWeibull()

double cugl::Random::getWeibull ( double  k,
double  lambda 
)
inline

Returns the next element in the Weibull distribution.

The mean is lambda * gamma(1+1/k) and the variance is

lambda^2 * (gamma(1+2/k)-gamma(1+1/k)^2)

where gamma() is the gamma function.

Parameters
kThe shape parameter
lambdaThe scale parameter
Returns
the next element in the Weibull distribution.

◆ init()

bool cugl::Random::init ( )

Initializes a psuedorandom number generator with the current time.

The random number generator is the classic 64 bit version implement here

http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
Returns
true if initialization was successful

◆ initWithArray()

bool cugl::Random::initWithArray ( Uint64 *  key,
size_t  len 
)

Initializes a psuedorandom number generator with the given keys

The random number generator is the classic 64 bit version implement here

http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c

Generators with the same key sequence will generate the same numbers.

Parameters
keyThe array of generator keys
lenThe key length
Returns
true if initialization was successful

◆ initWithSeed()

bool cugl::Random::initWithSeed ( Uint64  seed)

Initializes a psuedorandom number generator with the given seed

The random number generator is the classic 64 bit version implement here

http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.c

Generators with the same seed will generate the same numbers.

Parameters
seedThe generator seed
Returns
true if initialization was successful

◆ reset() [1/3]

void cugl::Random::reset ( )

Resets the random generator to use the current time as the seed.

The previous pseudo-random sequence will be discarded and replaced with the new one. Generation will start at the beginning of the new sequence.

◆ reset() [2/3]

void cugl::Random::reset ( Uint64 *  key,
size_t  len 
)

Resets the random generator to use the given keys.

The previous pseudo-random sequence will be discarded and replaced with the new one. Generation will start at the beginning of the new sequence.

Parameters
keyThe array of generator keys
lenThe key length

◆ reset() [3/3]

void cugl::Random::reset ( Uint64  seed)

Resets the random generator to use the given seed.

The previous pseudo-random sequence will be discarded and replaced with the new one. Generation will start at the beginning of the new sequence.

Parameters
seedThe new generator seed

◆ shuffle() [1/2]

template<typename T >
void cugl::Random::shuffle ( std::vector< T > &  data)
inline

Randomly shuffles the data in place

The function works on a vector.

Parameters
dataThe vector to shuffle

◆ shuffle() [2/2]

template<typename T >
void cugl::Random::shuffle ( T data,
size_t  len 
)
inline

Randomly shuffles the data in place

The function works on an array of data. The length must be specified.

Parameters
dataThe array to shuffle
lenThe number of elements in the array

The documentation for this class was generated from the following file: