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".
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.
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()() constThe function call operator is used to retrieve the next value from the generator.
This class uses an internal object of class BaseGen to generate
uniformly distributed integers on some closed interval.
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.
int low() constReturns the lower edge of the closed interval of numbers generated.
int high() constReturns 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.
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()() constThe function call operator returns a random int from the
appropriate range.
This class uses an internal object of class BaseGen to generate
uniformly distributed double values on some open interval.
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.
double low() constReturns the lower edge of the open interval of numbers generated.
double high() constReturns 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.
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()() constThe function call operator returns a random double from the
appropriate range.
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.
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.
double mean() constReturns the mean of the generator.
double stdv() constReturns 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.
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()() constThe function call operator returns a random double from the
appropriate distribution.