![]() |
CUGL 3.0
Cornell University Game Library
|
#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 > | |
T * | getChoice (T *data, size_t len) |
template<typename T > | |
T * | getChoice (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< Random > | alloc () |
static std::shared_ptr< Random > | allocWithSeed (Uint64 seed) |
static std::shared_ptr< Random > | allocWithArray (Uint64 *key, size_t len) |
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.
|
inline |
Creates an uninitialized generator with no sequence.
You must initialize the generator to create a pseudo-random sequence.
|
inline |
Deletes this generator, disposing all resources.
|
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
|
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.
key | The array of generator keys |
len | The key length |
|
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.
seed | The generator seed |
void cugl::Random::dispose | ( | ) |
Disposes the psuedo-random sequence for this generator
You must reinitialize the generator to use it.
|
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)
alpha | The first shape parameter (should be > 0) |
beta | The second shape parameter (should be > 0) |
|
inline |
Returns the next pseudorandom value true or false.
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.
data | The vector to select from |
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.
data | The array to select from |
len | The number of elements in the array |
|
inline |
Returns the next pseudorandom double in [0,1]
Both endpoints of the interval are included.
|
inline |
Returns the next pseudorandom double in [min,max]
Both endpoints of the interval are included. If min > max, the result is undefined.
|
inline |
Returns the next pseudorandom float in [min,max]
Both endpoints of the interval are included. If min > max, the result is undefined.
|
inline |
Returns the next pseudorandom signed integer in [min, max]
If min >= max, the result is undefined.
|
inline |
Returns the next pseudorandom integer in [min, max]
If min >= max, the result is undefined.
|
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.
|
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.
mu | The standard deviation |
|
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.
|
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).
alpha | The shape parameter (should be > 0) |
beta | The rate parameter (should be > 0) |
|
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.
|
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.
|
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.
|
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.
|
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.
mu | The mean |
sigma | The standard deviation |
|
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.
mu | The mean |
sigma | The standard deviation |
|
inline |
Returns the next pseudorandom double in (0,1)
Neither endpoint in the interval is included.
|
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.
|
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.
|
inline |
Returns the next pseudorandom signed integer in [min, max)
If min >= max, the result is undefined.
|
inline |
Returns the next pseudorandom integer in [min, max)
If min >= max, the result is undefined.
|
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.
xm | The scale parameter |
alpha | The shape parameter |
|
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.
|
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.
|
inline |
Returns the seed for the given generator.
If a key sequence was used in place of a seed, this method will return 0.
|
inline |
Returns the next pseudorandom integer in [-2^15, 2^15-1]
|
inline |
Returns the next pseudorandom integer in [-2^31, 2^31-1]
|
inline |
Returns the next pseudorandom integer in [-2^63, 2^63-1]
|
inline |
Returns the next pseudorandom integer in [-128,127]
|
inline |
Returns the next pseudorandom integer in [0, 2^16-1]
|
inline |
Returns the next pseudorandom integer in [0, 2^32-1]
|
inline |
Returns the next pseudorandom integer in [0, 2^64-1]
|
inline |
Returns the next pseudorandom integer in [0, 255]
|
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.
k | The shape parameter |
lambda | The scale parameter |
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
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.
key | The array of generator keys |
len | The key length |
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.
seed | The generator seed |
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.
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.
key | The array of generator keys |
len | The key length |
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.
seed | The new generator seed |
Randomly shuffles the data in place
The function works on a vector.
data | The vector to shuffle |
Randomly shuffles the data in place
The function works on an array of data. The length must be specified.
data | The array to shuffle |
len | The number of elements in the array |