...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
"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. 274328Depending on the requirements of the problem domain, different variations of random number generators are appropriate:
The goals for this library are the following:
operator()
returns a number.
In the following table, X
denotes a number generator
class returning objects of type T
, and u
is
a value of X
.
NumberGenerator
requirements  

expression  return type  pre/postcondition 
X::result_type  T  std::numeric_limits<T>::is_specialized is true,
T is LessThanComparable 
u.operator()()  T   
Note: The NumberGenerator requirements do not impose any restrictions on the characteristics of the returned numbers.
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 T
, and v
is
a const value of X
.
UniformRandomNumberGenerator
requirements  

expression  return type  pre/postcondition 
X::has_fixed_range  bool 
compiletime constant; if true , the range on which
the random numbers are uniformly distributed is known at compiletime
and members min_value and max_value
exist. Note: This flag may also be false due to
compiler limitations. 
X::min_value  T 
compiletime constant; min_value is equal to
v.min() 
X::max_value  T 
compiletime constant; max_value is equal to
v.max() 
v.min()  T 
tight lower bound on the set of all values returned by
operator() . The return value of this function shall not
change during the lifetime of the object. 
v.max()  T 
if std::numeric_limits<T>::is_integer , tight
upper bound on the set of all values returned by
operator() , otherwise, the smallest representable number
larger than the tight upper bound on the set of all values returned by
operator() . In any case, the return value of this
function shall not change during the lifetime of the
object. 
The member functions min
, max
, and
operator()
shall have amortized constant time complexity.
Note: For integer generators (i.e. integer T
),
the generated values x
fulfill min() <= x <=
max()
, for noninteger generators (i.e. noninteger
T
), the generated values x
fulfill
min() <= x < max()
.
Rationale: The range description with min
and
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
processing.
The range is a closed interval [min,max] for integers, because the
underlying type may not be able to represent the halfopen interval
[min,max+1). It is a halfopen interval [min, max) for nonintegers,
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. Use the
random_number_generator
adapter for that.
Rationale: operator()(long)
is not provided,
because mapping the output of some generator with integer range to a
different integer range is not trivial.
The class
random_device
is a model for a nondeterministic random number generator.
Note: This type of randomnumber 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.
Donald E. Knuth gives an extensive overview on pseudorandom number generation in his book "The Art of Computer Programming, Vol. 2, 3rd edition, AddisonWesley, 1997". The descriptions for the specific generators contain additional references.
Note: Because the state of a pseudorandom number generator is necessarily finite, the sequence of numbers returned by the generator will loop eventually.
In addition to the UniformRandomNumberGenerator requirements, a
pseudorandom number generator has some additional requirements. In
the following table, X
denotes a pseudorandom number
generator class returning objects of type T
,
x
is a value of T
, u
is a value
of X
, and v
is a const
value of
X
.
PseudoRandomNumberGenerator
requirements  

expression  return type  pre/postcondition 
X()    creates a generator in some implementationdefined state. Note: Several generators thusly created may possibly produce dependent or identical sequences of random numbers. 
explicit X(...)    creates a generator with userprovided state; the implementation shall specify the constructor argument(s) 
u.seed(...)  void  sets the current state according to the argument(s); at least functions with the same signature as the nondefault constructor(s) shall be provided. 
X::validation(x)  bool 
compares the precomputed and hardcoded 10001th element in the
generator's random number sequence with x . The generator
must have been constructed by its default constructor and
seed must not have been called for the validation to
be meaningful.

Note: The seed
member function is similar to the
assign
member function in STL containers. However, the
naming did not seem appropriate.
Classes which model a pseudorandom number generator shall also model
EqualityComparable, i.e. implement operator==
. Two
pseudorandom number generators are defined to be equivalent
if they both return an identical sequence of numbers starting from a
given state.
Classes which model a pseudorandom number generator should also model
the Streamable concept, i.e. implement operator<<
and operator>>
. If so,
operator<<
writes all current state of the
pseudorandom number generator to the given ostream
so
that operator>>
can restore the state at a later
time. The state shall be written in a platformindependent manner,
but it is assumed that the locale
s used for writing and
reading be the same.
The pseudorandom number generator with the restored state and the
original at the justwritten state shall be equivalent.
Classes which model a pseudorandom 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 pseudorandom number generators is
discouraged; they should always be passed by (nonconst
)
reference.
The classes
rand48
,
minstd_rand
,
and
mt19937
are models for a pseudorandom number generator.
Note: This type of randomnumber generator is useful for
numerics, games and testing. The nonzero arguments constructor(s)
and the seed()
member function(s) allow for a
userprovided state to be installed in the generator. This is useful
for debugging MonteCarlo algorithms and analyzing particular test
scenarios. The Streamable concept allows to save/restore the state of
the generator, for example to rerun a test suite at a later time.
X
denotes a random distribution
class returning objects of type T
, u
is a
value of X
, x
is a (possibly const)
value of X
, and e
is an lvalue of an
arbitrary type that meets the requirements of a uniform random number
generator, returning values of type U
.
Random distribution requirements
(in addition to number generator,
CopyConstructible , and Assignable ) 


expression  return type  pre/postcondition  complexity 
X::input_type 
U    compiletime 
u.reset() 
void 
subsequent uses of u do not depend on values
produced by e prior to invoking reset . 
constant 
u(e) 
T 
the sequence of numbers returned by successive invocations with
the same object e is randomly distributed with some
probability density function p(x) 
amortized constant number of invocations of e 
os << x 
std::ostream& 
writes a textual representation for the parameters and additional
internal data of the distribution x to os .
post: The os.fmtflags and fill character are
unchanged. 
O(size of state) 
is >> u 
std::istream& 
restores the parameters and additional internal data of the
distribution u .
pre: is provides a textual representation that was
previously written by operator<<
post: The is.fmtflags are unchanged. 
O(size of state) 
Additional requirements: The sequence of numbers produced by
repeated invocations of x(e)
does not change whether or
not os << x
is invoked between any of the
invocations x(e)
. If a textual representation
is written using os << x
and that representation
is restored into the same or a different object y
of the
same type using is >> y
, repeated invocations of
y(e)
produce the same sequence of random numbers as would
repeated invocations of x(e)
.
Note: Quasirandom number generators are useful for MonteCarlo integrations where specially crafted sequences of random numbers will make the approximation converge faster.
[Does anyone have a model?]