Xfce Foundation Classes
Main Page  | IndexNamespace List  |  Alphabetical List  |  Class List  |  File List


Public Member Functions
Xfc::G::Rand Class Reference

A GRand C++ wrapper interface. More...

#include <xfc/glib/rand.hh>

Inheritance diagram for Xfc::G::Rand:
Xfc::Object Xfc::Trackable

List of all members.

Public Member Functions

Constructors

Detailed Description

A GRand C++ wrapper interface.

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.


Constructor & Destructor Documentation

Constructs a new random number generator initialized with a seed taken either from /dev/urandom (if existing) or from the current time (as a fallback).

Xfc::G::Rand::Rand ( unsigned int  seed)

Constructs a new random number generator initialized with seed.

Parameters:
seedA 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.

Parameters:
seedA vector of seeds to initialize the random number generator.
Xfc::G::Rand::Rand ( const Rand src)

Copy constructor.

Parameters:
srcThe source random number generator.

Member Function Documentation

bool Xfc::G::Rand::get_bool ( ) const

Gets a random boolean value (this corresponds to a unbiased coin toss).

Returns:
A random bool value.
double Xfc::G::Rand::get_double ( ) const

Gets the next random double from the rand equally distributed over the range [0..1).

Returns:
A random number.
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).

Parameters:
beginThe lower closed bound of the interval.
endThe upper open bound of the interval.
Returns:
A random number.
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].

Returns:
A random number.
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].

Parameters:
beginThe lower closed bound of the interval.
endThe upper open bound of the interval.
Returns:
A random number.
Rand& Xfc::G::Rand::operator= ( const Rand src)

Assignment operator.

Parameters:
srcThe source random number generator.
void Xfc::G::Rand::set_seed ( unsigned int  seed)

Sets the seed for the random number generator to seed.

Parameters:
seedA 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.

Parameters:
seedThe vector to initialize random number generator with.

The vector can be of arbitrary size, though only the first 624 values are taken. This function is useful if you have many low entropy seeds, or if you require more then 32bits of actual entropy for your application.


The documentation for this class was generated from the following file:
Xfce Foundation Classes
Copyright © 2004-2005 The XFC Development Team XFC