ASL
|
A random number generator.
Generates uniformly distributed pseudo-random numbers, except in the normal()
functions.
The generator initially is seeded pseudorandomly. If you need a constant sequence you can create with a false argument, to prevent this or call seed().
For compatibility with older code, there is a global asl::random
object already random initialized ready for use. But it is recommended to use new Random objects when separate sequences or multithreading are needed.
#include <defs.h>
Public Member Functions | |
unsigned | get () |
Returns an integer pseudo-random number in the [0, 2^32-1] interval. | |
double | operator() (double m) |
Returns a floating point random number in the [0, m] interval. | |
double | operator() (double m, double M) |
Returns a floating point random number in the [m, M] interval. | |
float | operator() (float m) |
Returns a floating point random number in the [0, M] interval. | |
float | operator() (float m, float M) |
Returns a floating point random number in the [m, M] interval. | |
template<class T > | |
T | operator() (T m) |
Returns an integer random number in the [0, M] interval. | |
template<class T > | |
T | operator() (T m, T M) |
Returns an integer random number in the [m, M] interval. | |
double | normal () |
Returns a floating point random number with standard normal distribution. | |
double | normal (double m, double s) |
Returns a floating point random number with normal distribution with given mean and standard deviation. | |
float | normal (float m, float s) |
Returns a floating point random number with normal distribution with given mean and standard deviation. | |
bool | coin (double p=0.5) |
Returns true or false given a probability (by default it is 0.5, like flipping a coin) | |
void | seed (ULong s) |
Initializes the seed for the random functions. | |
void | init (bool fast=true) |
Initializes the seed for the random functions randomly (set fast=false for a high quality random seed) | |
template<typename T > | |
void | shuffle (T *a, int n) |
Shuffles an array of elements in place (n items starting at a). | |
template<typename E > | |
void | shuffle (E &a) |
Shuffles an array of elements in place. | |
Static Public Member Functions | |
static void | getBytes (void *buffer, int n) |
Fills a buffer with OS-provided random bytes or pseudo-random if that fails. | |