#include <RNG.h>
Public Member Functions | |
RNG (long s=1) | |
Creates a new pseudo random number instance and generates seed values for the three internal number generators. | |
void | seed (long s=time(NULL)) |
Uses "s" to generate the seed values for the three internal random number generators. | |
void | reset () |
Resets the current results of the three internal random number generators to the seed values. | |
long | genLong () |
Returns a new discrete uniformally distributed pseudo random number from the interval (0,1). | |
double | genDouble () |
Returns a new continuous uniformally distributed pseudo random number from the interval (0,1). | |
void | getStatus (unsigned &x, unsigned &y, unsigned &z) const |
Returns the current results of the three internal random number generators. | |
void | setStatus (const unsigned x, const unsigned y, const unsigned z) |
Sets the current results of the three internal random number generators. | |
virtual double | operator() () |
Returns a new continuous uniformally distributed pseudo random number of the interval (0,1). | |
Static Public Attributes | |
static RNG | globalRng |
A computer can only create "pseudo-random numbers", i.e. a sequence of numbers generated in some systematic way such that its statistical properties are as close as possible to those of true random numbers, e.g. negligible correlation between consecutive numbers. The most common methods are based on the "multiplicative congruential algorithm".
This basic algorithm is defined as:
The resulting integers are then divided by
to give uniformly distributed pseudo-random numbers lying in the interval (0,1).
The start number is also called the "seed".
As you can see the random numbers created are cyclic with a maximum frequency of . When choosing unintelligent numbers for
and
, the frequency will be further shortened.
The aim is to achieve a most possible large frequency.
Therefore, this class uses an alternative algorithm, the Wichman-Hill algorithm, that uses three generators:
From this the pseudo random numbers are generated as:
The random numbers are uniformally distributed in the interval (0,1).
By using intelligent numbers for and
for
a frequency of
is achieved.
For more information about this algorithm please refer to the BYTE magazine, March, 1987, pp. 127-128.
There will be one static instance of this class named globalRng. This instance can be used as base for the template class RandomVar from which all random generators used in library "#Rng" are derived.
This is done, because for every distribution simulation, where the distribution is not uniform, the uniformally distributed pseudo random numbers delivered by this class are transformed to the types of the other distributions.
So, if you have the uniformally distributed random numbers you can transform them into random numbers
that are distributed by the function
by applying the inverse function of
to
:
This is also called the inverse transformation.
RNG::RNG | ( | long | s = 1 |
) | [inline] |
Creates a new pseudo random number instance and generates seed values for the three internal number generators.
s is used as base for the generation of the seed numbers (see description of the class) for the three number generators.
s | the base for the generation of the three seed values, by default the value "1" is taken. |
void RNG::seed | ( | long | s = time( NULL ) |
) | [inline] |
Uses "s" to generate the seed values for the three internal random number generators.
s is used as base for the generation of the seed numbers (see description of the class) for the three number generators.
s | the base for the generation of the three seed values, by default the current system clock time is taken (i.e. the number of seconds since 00:00 hours, Jan 1, 1970 UTC). |
void RNG::reset | ( | ) | [inline] |
Resets the current results of the three internal random number generators to the seed values.
The current results (see description of the class) for the three number generators are set to the stored seed values
.
long RNG::genLong | ( | ) | [inline] |
Returns a new discrete uniformally distributed pseudo random number from the interval (0,1).
Generates and returns the current random number (see description of class) in the interval (0,1). The type of the random number will be "long", so the original continuous type of the random number will be transformed to a discrete type.
double RNG::genDouble | ( | ) | [inline] |
Returns a new continuous uniformally distributed pseudo random number from the interval (0,1).
Generates and returns the current random number (see description of class) in the interval (0,1). The type of the continuous random number will be "double".
void RNG::getStatus | ( | unsigned & | x, | |
unsigned & | y, | |||
unsigned & | z | |||
) | const [inline] |
Returns the current results of the three internal random number generators.
Stores the current values of (see description of the class) in x, y and z.
x | the current result ![]() | |
y | the current result ![]() | |
z | the current result ![]() |
void RNG::setStatus | ( | const unsigned | x, | |
const unsigned | y, | |||
const unsigned | z | |||
) | [inline] |
Sets the current results of the three internal random number generators.
Sets the current values of (see description of the class), that will be used as base for the generation of the next random number.
x | the new current result ![]() | |
y | the new current result ![]() | |
z | the new current result ![]() |
virtual double RNG::operator() | ( | ) | [inline, virtual] |
Returns a new continuous uniformally distributed pseudo random number of the interval (0,1).
Generates and returns the current random number (see description of the class) as continuous value of type "double".
RNG RNG::globalRng [static] |