next up previous contents index
Next: Random Variates ( random_variate Up: Simple Data Types and Previous: String Output Streams (   Contents   Index


Random Sources ( random_source )

Definition

An instance of type random_source is a random source. It allows to generate uniformly distributed random bits, characters, integers, and doubles. It can be in either of two modes: In bit mode it generates a random bit string of some given length p ( 1 < = p < = 31 ) and in integer mode it generates a random integer in some given range [low..high] ( low < = high < low + 231 ). The mode can be changed any time, either globally or for a single operation. The output of the random source can be converted to a number of formats (using standard conversions).

#include < LEDA/core/random_source.h >

Creation

random_source S creates an instance S of type random_source, puts it into bit mode, and sets the precision to 31.

random_source S(int p) creates an instance S of type random_source, puts it into bit mode, and sets the precision to p ( 1 < = p < = 31 ).

random_source S(int low, int high) creates an instance S of type random_source, puts it into integer mode, and sets the range to [low..high] .

Operations

unsigned long S.get() returns a random unsigned long integer (32 bits on 32-bit systems or on LLP64 systems and 64 bits on other 64-bit systems).

void S.set_seed(int s) resets the seed of the random number generator to s .

int S.reinit_seed() generates and sets a new seed s . The return value is s .

void S.set_range(int low, int high)
    sets the mode to integer mode and changes the range to [low..high] .

int S.set_precision(int p) sets the mode to bit mode, changes the precision to p bits and returns previous precision.

int S.get_precision() returns current precision of S.

random_source& S > > char& x extracts a character x of default precision or range and returns S, i.e., it first generates an unsigned integer of the desired precision or in the desired range and then converts it to a character (by standard conversion).

random_source& S > > unsigned char& x extracts an unsigned character x of default precision or range and returns S.

random_source& S > > int& x extracts an integer x of default precision or range and returns S.

random_source& S > > long& x extracts a long integer x of default precision or range and returns S.

random_source& S > > unsigned int& x extracts an unsigned integer x of default precision or range and returns S.

random_source& S > > unsigned long& x extracts a long unsigned integer x of default precision or range and returns S.

random_source& S > > double& x extracts a double precision floating point number x in [0, 1] , i.e, u/(231 - 1) where u is a random integer in [0..231 - 1] , and returns S.

random_source& S > > float& x extracts a single precision floating point number x in [0, 1] , i.e, u/(231 - 1) where u is a random integer in [0..231 - 1] , and returns S.

random_source& S > > bool& b extracts a random boolean value (true or false).

int S() returns an integer of default precision or range.

int S(int prec) returns an integer of supplied precision prec .

int S(int low, int high) returns an integer from the supplied range [low..high] .


next up previous contents index
Next: Random Variates ( random_variate Up: Simple Data Types and Previous: String Output Streams (   Contents   Index
Christian Uhrig 2017-04-07