In this section we will simply review some possible alternatives for
pseudo-random number generators. These go from the simplest
``congruential'' or ``power residue'' algorithm to more
sophisticated ones that can be found
in the literature. We will limit ourselves to understand this simple
example. We want to generate a sequence over an
interval . You multiply the previous random number by a
constant
, add on another constant , take the modulus by , and then keep
just the fractional part, the reminder, as the next rendom number
:
As an example, let us pick ,, and . We obtain the
sequence:
(257) | |||
(258) | |||
(259) | |||
(260) | |||
(261) |
The C++ Standard Library provides a psuedo-random number generator. It provides a function to initialize the seed of the sequence:
srand(size_t seed); and the actual call to retrieve a new random number
rand();
The generator provides a sequence between 0 and RAND_MAX, which is a large integer that deppends on the implementation. A common way to generate independent sequences is to use the internal clock of the computer to generate a relatively random seed.
For more on random number generators read Knuth, Numerical Recipes.