Random numbers are required in a number of different problem domains, such as
"Numerical Recipes in C: The art of scientific computing", William H. Press, Saul A. Teukolsky, William A. Vetterling, Brian P. Flannery, 2nd ed., 1992, pp. 274-328Depending on the requirements of the problem domain, different variations of random number generators are appropriate:
The goals for this library are the following:
A number generator is a function object (std:20.3
[lib.function.objects]) that takes zero arguments. Each call to
operator() returns a number. In the following table,
X denotes a number generator class returning objects of type
u is a value of
Note: The NumberGenerator requirements do not impose any restrictions on the characteristics of the returned numbers.
A uniform random number generator is a NumberGenerator that provides a sequence of random numbers uniformly distributed on a given range. The range can be compile-time fixed or available (only) after run-time construction of the object.
The tight lower bound of some (finite) set S is the (unique) member l in S, so that for all v in S, l <= v holds. Likewise, the tight upper bound of some (finite) set S is the (unique) member u in S, so that for all v in S, v <= u holds.
In the following table,
X denotes a number generator class
returning objects of type
v is a const
||compile-time constant; if
||tight lower bound on the set of all values returned by
The member functions
operator() shall have amortized constant time complexity.
Note: For integer generators (i.e. integer
min() <= x <=
max(), for non-integer generators (i.e. non-integer
the generated values
min() <= x <
Rationale: The range description with
max serves two purposes. First, it allows scaling of the
values to some canonical range, such as [0..1). Second, it describes the
significant bits of the values, which may be relevant for further
The range is a closed interval [min,max] for integers, because the underlying type may not be able to represent the half-open interval [min,max+1). It is a half-open interval [min, max) for non-integers, because this is much more practical for borderline cases of continuous distributions.
Note: The UniformRandomNumberGenerator concept does not require
operator()(long) and thus it does not fulfill the
RandomNumberGenerator (std:25.2.11 [lib.alg.random.shuffle]) requirements.
adapter for that.
operator()(long) is not provided, because
mapping the output of some generator with integer range to a different
integer range is not trivial.
A non-deterministic uniform random number generator is a UniformRandomNumberGenerator that is based on some stochastic process. Thus, it provides a sequence of truly-random numbers. Examples for such processes are nuclear decay, noise of a Zehner diode, tunneling of quantum particles, rolling a die, drawing from an urn, and tossing a coin. Depending on the environment, inter-arrival times of network packets or keyboard events may be close approximations of stochastic processes.
random_device is a model for
a non-deterministic random number generator.
Note: This type of random-number generator is useful for security applications, where it is important to prevent that an outside attacker guesses the numbers and thus obtains your encryption or authentication key. Thus, models of this concept should be cautious not to leak any information, to the extent possible by the environment. For example, it might be advisable to explicitly clear any temporary storage as soon as it is no longer needed.
A pseudo-random number generator is a UniformRandomNumberGenerator which provides a deterministic sequence of pseudo-random numbers, based on some algorithm and internal state. Linear congruential and inversive congruential generators are examples of such pseudo-random number generators. Often, these generators are very sensitive to their parameters. In order to prevent wrong implementations from being used, an external testsuite should check that the generated sequence and the validation value provided do indeed match.
Donald E. Knuth gives an extensive overview on pseudo-random number generation in his book "The Art of Computer Programming, Vol. 2, 3rd edition, Addison-Wesley, 1997". The descriptions for the specific generators contain additional references.
Note: Because the state of a pseudo-random number generator is necessarily finite, the sequence of numbers returned by the generator will loop eventually.
In addition to the UniformRandomNumberGenerator requirements, a
pseudo-random number generator has some additional requirements. In the
X denotes a pseudo-random number generator
class returning objects of type
x is a value
u is a value of
v is a
const value of
||-||creates a generator in some implementation-defined state. Note: Several generators thusly created may possibly produce dependent or identical sequences of random numbers.|
||-||creates a generator with user-provided state; the implementation shall specify the constructor argument(s)|
||void||sets the current state according to the argument(s); at least functions with the same signature as the non-default constructor(s) shall be provided.|
||compares the pre-computed and hardcoded 10001th element in the
generator's random number sequence with
seed member function is similar to the
assign member function in STL containers. However, the naming
did not seem appropriate.
Classes which model a pseudo-random number generator shall also model
EqualityComparable, i.e. implement
pseudo-random number generators are defined to be equivalent if
they both return an identical sequence of numbers starting from a given
Classes which model a pseudo-random number generator should also model
the Streamable concept, i.e. implement
operator>>. If so,
all current state of the pseudo-random number generator to the given
ostream so that
operator>> can restore the
state at a later time. The state shall be written in a platform-independent
manner, but it is assumed that the
locales used for writing
and reading be the same. The pseudo-random number generator with the
restored state and the original at the just-written state shall be
Classes which model a pseudo-random number generator may also model the
CopyConstructible and Assignable concepts. However, note that the sequences
of the original and the copy are strongly correlated (in fact, they are
identical), which may make them unsuitable for some problem domains. Thus,
copying pseudo-random number generators is discouraged; they should always
be passed by (non-
are models for a pseudo-random number generator.
Note: This type of random-number generator is useful for
numerics, games and testing. The non-zero arguments constructor(s) and the
seed() member function(s) allow for a user-provided state to
be installed in the generator. This is useful for debugging Monte-Carlo
algorithms and analyzing particular test scenarios. The Streamable concept
allows to save/restore the state of the generator, for example to re-run a
test suite at a later time.
A random distribution produces random numbers distributed according to
some distribution, given uniformly distributed random values as input. In
the following table,
X denotes a random distribution class
returning objects of type
u is a value of
x is a (possibly const) value of
e is an lvalue of an arbitrary type that
meets the requirements of a uniform random number generator, returning
values of type
|Random distribution requirements (in
addition to number generator,
||subsequent uses of
||the sequence of numbers returned by successive invocations with the
||amortized constant number of invocations of
||writes a textual representation for the parameters and additional
internal data of the distribution
|O(size of state)|
||restores the parameters and additional internal data of the
|O(size of state)|
Additional requirements: The sequence of numbers produced by repeated
x(e) does not change whether or not
<< x is invoked between any of the invocations
x(e). If a textual representation is written using
<< x and that representation is restored into the same or a
y of the same type using
y, repeated invocations of
y(e) produce the same
sequence of random numbers as would repeated invocations of
A quasi-random number generator is a Number Generator which provides a deterministic sequence of numbers, based on some algorithm and internal state. The numbers do not have any statistical properties (such as uniform distribution or independence of successive values).
Note: Quasi-random number generators are useful for Monte-Carlo integrations where specially crafted sequences of random numbers will make the approximation converge faster.
[Does anyone have a model?]
Revised 05 December, 2006
Copyright © 2000-2003 Jens Maurer
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)