3.7. BaseGen

This is a class for generating pseudorandom numbers. It is based on the KISS (Keep It Simple, Stupid) algorithm developed by George Marsaglia.

It combines two multiply-with-carry generators with a 3-shift register generator, and a congruential generator, using addition and bitwise exclusive-or. It should have a period of about 2^123, or about 1.0E+37. This class produces unsigned int values by default, but if your platform does not use 32 bit integers, then the type used by the generator should be changed to a 32 bit type. This class is used as the internal generator for the IntGen, UniformGen and NormalGen classes. To use any of these classes, include the header file "random.h".

3.7.1. Constructors.

explicit BaseGen(UINT32 seed = 0)

This initializes the random number with seed. Since the state of the generator is defined by four numbers, this initialization is done by initializing the congruential generator portion of the generator with seed, and then iterating it four times to produce the initial states for all four components. If seed is zero, or no seed is supplied, then the current value of the time() function is used.

BaseGen(const BaseGen& gen)

The copy constructer initializes the generator with a seed from gen.

3.7.2. Overloaded operators.

BaseGen& operator=(const BaseGen& gen)

The assignment operator reinitializes the generator with a seed from gen. Note that g = g is perfectly OK, and will simply reseed the generator with the next number it generates.

UINT32 operator()() const

The function call operator is used to retrieve the next value from the generator.

3.8. IntGen

This class uses an internal object of class BaseGen to generate uniformly distributed integers on some closed interval.

3.8.1. Constructors.

explicit IntGen(UINT32 seed = 0, int a = 0, int b = 0)

This initializes the internal BaseGen with seed. The values a and b denote the closed interval of numbers to be generated. All three parameters are optional. If no parameters are given, then the generator is seeded with the time() function, and the interval is set to (0, 1). If one parameter is given, then it is taken to be the seed, and the interval is set to (0, 1). If two parameters are given, then the first is the seed, and the second is used for one end of the interval, giving either (0, a) or (a, 0), depending on whether a is positive or negative. If a is zero, then the interval is set to (0, 1). If all three parameters are given, then the interval is either set to (a, b), (b, a), or (0, 1), depending on whether a < b, a > b, or a == b. If a seed of zero is given, then the generator is initialized with the time() function.

IntGen(const IntGen& gen)

The copy constructor initializes the new generator using the BaseGen assignment operator to initialize the internal generator with the internal generator of gen. The interval of generated values is also copied from gen.

3.8.2. Member functions.

int low() const

Returns the lower edge of the closed interval of numbers generated.

int high() const

Returns the upper edge of the closed interval of numbers generated.

void set_range(int a = 0, int b = 0)

Sets the interval of numbers generated to (a, b), (b, a), or (0, 1), depending on whether a < b, a > b, or a == b. Both parameters are optional. If no parameters are given, the interval is set to (0, 1). If only one parameter is given, then the missing parameter is assumed to be zero.

3.8.3. Overloaded operators.

IntGen& operator=(const IntGen& gen)

The assignment operator reinitializes the generator by using the BaseGen assignment operator of gen on the internal generator. It also sets the output range equal to that of gen.

int operator()() const

The function call operator returns a random int from the appropriate range.

3.9. UniformGen

This class uses an internal object of class BaseGen to generate uniformly distributed double values on some open interval.

3.9.1. Constructors.

explicit UniformGen(UINT32 seed = 0, double a = 0.0, double b = 0.0)

This initializes the internal BaseGen with seed. The values a and b denote the open interval of numbers to be generated. All three parameters are optional. If no parameters are given, then the generator is seeded with the time() function, and the interval is set to (0.0, 1.0). If one parameter is given, then it is taken to be the seed, and the interval is set to (0.0, 1.0). If two parameters are given, then the first is the seed, and the second is used for one end of the interval, giving either (0.0, a) or (a, 0.0), depending on whether a is positive or negative. If a is zero, then the interval is set to (0.0, 1.0). If all three parameters are given, then the interval is either set to (a, b), (b, a), or (0.0, 1.0), depending on whether a < b, a > b, or a == b. If a seed of zero is given, then the generator is initialized with the time() function.

UniformGen(const UniformGen& gen)

The copy constructor initializes the new generator using the BaseGen assignment operator to initialize the internal generator with the internal generator of gen. The interval of generated values is also copied from gen.

3.9.2. Member functions.

double low() const

Returns the lower edge of the open interval of numbers generated.

double high() const

Returns the upper edge of the open interval of numbers generated.

void set_range(double a = 0.0, double b = 0.0)

Sets the interval of numbers generated to (a, b), (b, a), or (0.0, 1.0), depending on whether a < b, a > b, or a == b. Both parameters are optional. If no parameters are given, the interval is set to (0.0, 1.0). If only one parameter is given, then the missing parameter is assumed to be zero.

3.9.3. Overloaded operators.

UniformGen& operator=(const UniformGen& gen)

The assignment operator reinitializes the generator by using the BaseGen assignment operator of gen on the internal generator. It also sets the output range equal to that of gen.

double operator()() const

The function call operator returns a random double from the appropriate range.

3.10. NormalGen

This class uses an internal object of class BaseGen to generate normally distributed double values with a given mean and standard deviation. It uses the Monty Python method for generating these numbers, which is much faster than the commonly used polar and Box-Muller methods.

3.10.1. Constructors.

explicit NormalGen(UINT32 seed = 0, double m = 0.0, double stdv = 1.0)

This initializes the internal generator with seed, and sets the mean and standard deviation of the generator to m and stdv, respectively. If seed is zero or not given, then the time() function is used for initialization.

NormalGen(const NormalGen& gen)

The copy constructor initializes the new generator using the BaseGen assignment operator to initialize the internal generator with the internal generator of gen. The mean and standard deviation are also copied from gen.

3.10.2. Member functions.

double mean() const

Returns the mean of the generator.

double stdv() const

Returns the standard deviation of the generator.

void set_range(double m = 0.0, double stdv = 1.0)

Sets the mean and standard deviation of the generator to m and stdv. Both parameters are optional, with defaults of 0.0 and 1.0, respectively.

3.10.3. Overloaded operators.

NormalGen& operator=(const NormalGen& gen)

The assignment operator reinitializes the generator by using the BaseGen assignment operator of gen on the internal generator. It also sets the mean and standard deviation equal to that of gen.

double operator()() const

The function call operator returns a random double from the appropriate distribution.

Back to Classes