Xfce
Foundation Classes |
Main Page | Index | Namespace List | Alphabetical List | Class List | File List |
#include <xfc/glib/rand.hh>
Inheritance diagram for Xfc::G::Rand:
Rand is a portable, fast and good pseudo-random number generator (PRNG). It uses the Mersenne Twister PRNG, which was originally developed by Makoto Matsumoto and Takuji Nishimura. Further information can be found at http://www.math.keio.ac.jp/~matumoto/emt.html.
If you just need a random number, you can simply call the G::random* methods, which will create a globally used Rand object and use its methods internally. Whenever you need a stream of reproducible random numbers, you should create a Rand object yourself and call its methods directly, which will also be slightly faster. Initializing a Rand with a certain seed will produce exactly the same series of random numbers on all platforms. This can thus be used as a seed for games.
The Rand range methods will return high quality equally distributed random numbers, whereas for example the (G::Random::get_int()%max) approach often doesn't yield equally distributed numbers.
GLib changed the seeding algorithm for the pseudo-random number generator Mersenne Twister, as used by GRand and GRandom. This was necessary, because some seeds would yield very bad pseudo-random streams. Also the pseudo-random integers generated by Rand range methods will have a slightly better equal distribution with the new version of GLib.
The original seeding and generation algorithms, as found in GLib 2.0.x, can be used instead of the new ones by setting the environment variable G_RANDOM_VERSION to the value of '2.0'. Use the GLib-2.0 algorithms only if you have sequences of numbers generated with Glib-2.0 that you need to reproduce exactly.
Xfc::G::Rand::Rand | ( | unsigned int | seed | ) |
Constructs a new random number generator initialized with seed.
seed | A value to initialize the random number generator. |
Xfc::G::Rand::Rand | ( | const std::vector< unsigned int > & | seed | ) |
Constructs a new random number generator initialized with seed.
seed | A vector of seeds to initialize the random number generator. |
Xfc::G::Rand::Rand | ( | const Rand & | src | ) |
Copy constructor.
src | The source random number generator. |
Assignment operator.
src | The source random number generator. |
bool Xfc::G::Rand::get_bool | ( | ) | const |
Gets a random boolean value (this corresponds to a unbiased coin toss).
unsigned int Xfc::G::Rand::get_int | ( | ) | const |
Gets the next random unsigned integer from the rand equally distributed over the range [0.
.2^32-1].
int Xfc::G::Rand::get_int_range | ( | int | begin, | |
int | end | |||
) | const |
Gets the next random integer from the rand equally distributed over the range [begin.
.end-1].
begin | The lower closed bound of the interval. | |
end | The upper open bound of the interval. |
double Xfc::G::Rand::get_double | ( | ) | const |
Gets the next random double from the rand equally distributed over the range [0.
.1).
double Xfc::G::Rand::get_double_range | ( | double | begin, | |
double | end | |||
) | const |
Gets the next random double from the rand equally distributed over the range [begin.
.end).
begin | The lower closed bound of the interval. | |
end | The upper open bound of the interval. |
void Xfc::G::Rand::set_seed | ( | unsigned int | seed | ) |
Sets the seed for the random number generator to seed.
seed | A value to reinitialize the random number generator. |
void Xfc::G::Rand::set_seed | ( | const std::vector< unsigned int > & | seed | ) |
Initializes the random number generator with a vector of unsigned int.
seed | The vector to initialize random number generator with. |
Copyright © 2004-2005 The XFC Development Team | XFC
4.3 |