Random number functions. More...
Functions | |
template<class T > | |
T & | cond (T &v, const T &va, const T &vb, float pab, float pba) |
Conditional probability with two values. | |
template<class T > | |
T | geom (int n, T mul, T start=1, float p=0.5) |
Returns ith element in geometric series. | |
template<class T > | |
T | neg (T val, float prob=0.5f) |
Returns value negated with probability, otherwise the value. | |
template<class T > | |
const T & | pick (const T &val1, const T &val2, float prob=0.5f) |
Returns val1 with probability, otherwise val2. | |
template<class T > | |
bool | prob (T &rng, float p=0.5f) |
Returns true with a probability of p. | |
bool | prob (float p=0.5f) |
Returns true with a probability of p. | |
bool | prob (char c) |
Characters 0-8 return true with probability c/8. | |
void | push (uint32_t seed=0) |
Push current RNG state onto stack (stack size = 1). | |
void | pop () |
Pop RNG state from stack. | |
template<class T > | |
void | permute (T *arr, uint32_t len) |
Randomly permutes (shuffles) elements in array. | |
float | quan (uint32_t q) |
Returns value in [0, 1) quantized by q divisions. | |
template<class T > | |
T | quanOct (uint32_t q, T o) |
Returns value in [o, 2*o) quantized by q divisions. | |
void | seed (uint32_t value=0) |
Seed global PRNG. If seed is 0, then the system time (in seconds) is used. | |
template<class T > | |
void | set (T *arr, uint32_t len, uint32_t num, T val=1) |
Randomly set a certain amount of elements to a value. | |
template<class T > | |
void | thin (T *arr, uint32_t len, float prob=0.5f) |
Zeroes elements according to a probability. | |
template<class T > | |
T | uniExc (const T &exc, const T &max, const T &min=T(0)) |
Returns uniform random within interval [min, max) excluding 'exc' argument. | |
template<class T > | |
uint32_t | weighted (T *weights, uint32_t num, T weightsSum=(T) 1) |
Returns random integer in [0, num) according to weights (a PDF). |
Random number functions.
Unless specified, these operations use an internal Tausworthe RNG, favoring quality over a slight deficiency in speed compared to a linear congruential RNG.
The following template functions are available:
// Returns random number linearly mapped to [bound1, bound2) T distr(T bound2, T bound1 = 0); // Fills array with random numbers in [0, 1) void distr(T * dst, uint32_t len); // Fills array with random numbers linearly mapped to [bound1, bound2) void distr(T * dst, uint32_t len, T bound2, T bound1 = 0); // Copies random elements from 'src' to 'dst'. void distr(T * dst, uint32_t len, T * src, uint32_t srcLen); *
where "distr" is one of the following random number distributions:
add2 - sum of 2 uniform values (triangle)
add2I - inverse pdf of add2
add3 - sum of 3 uniform values (2nd order normal curve)
binS - uniform signed binary
lin - linear decrease
mul2 - product of 2 uniform values
pow2 - uniform value squared
pow3 - uniform value cubed
uni - uniform value
Since casts between floats and generic types are used in the ranged varieties, signed integer bounds will always be exclusive.
T & cond | ( | T & | v, |
const T & | va, | ||
const T & | vb, | ||
float | pab, | ||
float | pba | ||
) |
Conditional probability with two values.
'pab' and 'pba' are the probabilities of a given b and b given a.
bool prob | ( | char | c | ) |
Characters 0-8 return true with probability c/8.
In addition to numeric characters, '/' returns true and '.' returns false.
void push | ( | uint32_t | seed = 0 | ) |
Push current RNG state onto stack (stack size = 1).
After pushing, the current RNG is seeded with 'seed' unless 'seed' = 0.
uint32_t weighted | ( | T * | weights, |
uint32_t | num, | ||
T | weightsSum = (T)1 |
||
) |
Returns random integer in [0, num) according to weights (a PDF).
If the weights are not normalized, then the proper weightsSum must be passed in.