![]() |
Home | Libraries | People | FAQ | More |
This library provides several pseudo-random
number generators. The quality of a pseudo
random number generator crucially depends on both the algorithm and
its parameters. This library implements the algorithms as class templates
with template value parameters, hidden in namespace
boost::random
. Any particular choice of parameters
is represented as the appropriately specializing typedef
in namespace boost
.
Pseudo-random number generators should not be constructed (initialized) frequently during program execution, for two reasons. First, initialization requires full initialization of the internal state of the generator. Thus, generators with a lot of internal state (see below) are costly to initialize. Second, initialization always requires some value used as a "seed" for the generated sequence. It is usually difficult to obtain several good seed values. For example, one method to obtain a seed is to determine the current time at the highest resolution available, e.g. microseconds or nanoseconds. When the pseudo-random number generator is initialized again with the then-current time as the seed, it is likely that this is at a near-constant (non-random) distance from the time given as the seed for first initialization. The distance could even be zero if the resolution of the clock is low, thus the generator re-iterates the same sequence of random numbers. For some applications, this is inappropriate.
Note that all pseudo-random
number generators described below are CopyConstructible
and Assignable. Copying
or assigning a generator will copy all its internal state, so the original
and the copy will generate the identical sequence of random numbers. Often,
such behavior is not wanted. In particular, beware of the algorithms from
the standard library such as std::generate
.
They take a functor argument by value, thereby invoking the copy constructor
when called.
The following table gives an overview of some characteristics of the generators. The cycle length is a rough estimate of the quality of the generator; the approximate relative speed is a performance measure, higher numbers mean faster random number generation.
Table 1.5. generators
generator |
length of cycle |
approx. memory requirements |
approx. speed compared to fastest |
comment |
---|---|---|---|---|
231-2 |
|
16% |
- |
|
231-2 |
|
16% |
- |
|
248-1 |
|
64% |
- |
|
approx. 261 |
|
7% |
- |
|
? |
|
12% |
- |
|
? |
|
37% |
- |
|
~288 |
|
100% |
- |
|
231-1 |
|
2% |
good uniform distribution in several dimensions |
|
211213-1 |
|
100% |
good uniform distribution in up to 350 dimensions |
|
219937-1 |
|
93% |
good uniform distribution in up to 623 dimensions |
|
219937-1 |
|
38% |
good uniform distribution in up to 311 dimensions |
|
~232000 |
|
59% |
- |
|
~267000 |
|
59% |
- |
|
~2120000 |
|
61% |
- |
|
~2170000 |
|
62% |
- |
|
~2230000 |
|
59% |
- |
|
~2510000 |
|
61% |
- |
|
~21050000 |
|
59% |
- |
|
~21200000 |
|
61% |
- |
|
~22300000 |
|
59% |
- |
|
~10171 |
|
5% |
- |
|
~10171 |
|
3% |
- |
|
~10171 |
|
5% |
- |
|
~10171 |
|
3% |
- |
|
~10171 |
|
5% |
- |
|
~10171 |
|
3% |
- |
|
~10171 |
|
5% |
- |
|
~10171 |
|
3% |
- |
|
~10171 |
|
5% |
- |
|
~10171 |
|
3% |
- |
As observable from the table, there is generally a quality/performance/memory trade-off to be decided upon when choosing a random-number generator. The multitude of generators provided in this library allows the application programmer to optimize the trade-off with regard to his application domain. Additionally, employing several fundamentally different random number generators for a given application of Monte Carlo simulation will improve the confidence in the results.
If the names of the generators don't ring any bell and you have no idea which
generator to use, it is reasonable to employ mt19937
for a start: It is fast and has acceptable quality.
![]() |
Note |
---|---|
These random number generators are not intended for use in applications
where non-deterministic random numbers are required. See |