Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Generators

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

minstd_rand0

231-2

sizeof(int32_t)

16%

-

minstd_rand

231-2

sizeof(int32_t)

16%

-

rand48

248-1

sizeof(uint64_t)

64%

-

ecuyer1988

approx. 261

2*sizeof(int32_t)

7%

-

knuth_b

?

257*sizeof(uint32_t)

12%

-

kreutzer1986

?

98*sizeof(uint32_t)

37%

-

taus88

~288

3*sizeof(uint32_t)

100%

-

hellekalek1995

231-1

sizeof(int32_t)

2%

good uniform distribution in several dimensions

mt11213b

211213-1

352*sizeof(uint32_t)

100%

good uniform distribution in up to 350 dimensions

mt19937

219937-1

625*sizeof(uint32_t)

93%

good uniform distribution in up to 623 dimensions

mt19937_64

219937-1

312*sizeof(uint64_t)

38%

good uniform distribution in up to 311 dimensions

lagged_fibonacci607

~232000

607*sizeof(double)

59%

-

lagged_fibonacci1279

~267000

1279*sizeof(double)

59%

-

lagged_fibonacci2281

~2120000

2281*sizeof(double)

61%

-

lagged_fibonacci3217

~2170000

3217*sizeof(double)

62%

-

lagged_fibonacci4423

~2230000

4423*sizeof(double)

59%

-

lagged_fibonacci9689

~2510000

9689*sizeof(double)

61%

-

lagged_fibonacci19937

~21050000

19937*sizeof(double)

59%

-

lagged_fibonacci23209

~21200000

23209*sizeof(double)

61%

-

lagged_fibonacci44497

~22300000

44497*sizeof(double)

59%

-

ranlux3

~10171

24*sizeof(int)

5%

-

ranlux4

~10171

24*sizeof(int)

3%

-

ranlux64_3

~10171

24*sizeof(int64_t)

5%

-

ranlux64_4

~10171

24*sizeof(int64_t)

3%

-

ranlux3_01

~10171

24*sizeof(float)

5%

-

ranlux4_01

~10171

24*sizeof(float)

3%

-

ranlux64_3_01

~10171

24*sizeof(double)

5%

-

ranlux64_4_01

~10171

24*sizeof(double)

3%

-

ranlux24

~10171

24*sizeof(uint32_t)

5%

-

ranlux48

~10171

12*sizeof(uint64_t)

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] Note

These random number generators are not intended for use in applications where non-deterministic random numbers are required. See random_device for a choice of (hopefully) non-deterministic random number generators.


PrevUpHomeNext