The library defines a collection of random number engine classes and adaptors that use differing mathematical approaches to generating pseudorandom numbers. The library also defines a collection of distribution templates that provide numbers according to various probability distributions. Both the engines and the distributions have names that correspond to their mathematical properties.
The specifics of how these classes generate numbers is well beyond the scope of this Primer. In this section, we’ll list the engine and distribution types, but the reader will need to consult other resources to learn how to use these types.
With the exception of the bernouilli_distribution
, which always generates type bool
, the distribution types are templates. Each of these templates takes a single type parameter that names the result type that the distribution will generate.
The distribution classes differ from other class templates we’ve used in that the distribution types place restrictions on the types we can specify for the template type. Some distribution templates can be used to generate only floating-point numbers; others can be used to generate only integers.
In the following descriptions, we indicate whether a distribution generates floating-point numbers by specifying the type as template_name
<RealT>
. For these templates, we can use float
, double
, or long double
in place of RealT
. Similarly, IntT
requires one of the built-in integral types, not including bool
or any of the char
types. The types that can be used in place of IntT
are short
, int
, long
, long long
, unsigned short
, unsigned int
, unsigned long
, or unsigned long long
.
The distribution templates define a default template type parameter (§ 17.4.2, p. 750). The default for the integral distributions is int
; the default for the classes that generate floating-point numbers is double
.
The constructors for each distribution has parameters that are specific to the kind of distribution. Some of these parameters specify the range of the distribution. These ranges are always inclusive, unlike iterator ranges.
uniform_int_distribution<IntT> u(m, n);
uniform_real_distribution<RealT> u(x, y);
Generates values of the specified type in the given inclusive range. m
(or x
) is the smallest number that can be returned; n
(or y
) is the largest. m
defaults to 0; n
defaults to the maximum value that can be represented in an object of type IntT
. x
defaults to 0.0 and y
defaults to 1.0.
bernoulli_distribution b(p);
Yields true
with given probability p
; p
defaults to 0.5.
binomial_distribution<IntT> b(t, p);
Distribution computed for a sample size that is the integral value t
, with probability p
; t
defaults to 1 and p
defaults to 0.5.
geometric_distribution<IntT> g(p);
Per-trial probability of success p
; p
defaults to 0.5.
negative_binomial_distribution<IntT> nb(k, p);
Integral value k
trials with probability of success p
; k
defaults to 1 and p
to 0.5.
poisson_distribution<IntT> p(x);
Distribution around double
mean x
.
exponential_distribution<RealT> e(lam);
Floating-point valued lambda lam
; lam
defaults to 1.0.
gamma_distribution<RealT> g(a, b);
With alpha (shape) a
and beta (scale) b
; both default to 1.0.
weibull_distribution<RealT> w(a, b);
With shape a
and scale b
; both default to 1.0.
extreme_value_distribution<RealT> e(a, b);
a
defaults to 0.0 and b
defaults to 1.0.
normal_distribution<RealT> n(m, s);
Mean m
and standard deviation s
; m
defaults to 0.0, s
to 1.0.
lognormal_distribution<RealT> ln(m, s);
Mean m
and standard deviation s
; m
defaults to 0.0, s
to 1.0.
chi_squared_distribution<RealT> c(x);
x
degrees of freedom; defaults to 1.0.
cauchy_distribution<RealT> c(a, b);
Location a
and scale b
default to 0.0 and 1.0, respectively.
fisher_f_distribution<RealT> f(m, n);
m
and n
degrees of freedom; both default to 1.
student_t_distribution<RealT> s(n);
n
degrees of freedom; n
defaults to 1.
discrete_distribution<IntT> d(i, j);
discrete_distribution<IntT> d{il};
i
and j
are input iterators to a sequence of weights; il
is a braced list of weights. The weights must be convertible to double
.
piecewise_constant_distribution<RealT> pc(b, e, w);
b
, e
, and w
are input iterators.
piecewise_linear_distribution<RealT> pl(b, e, w);
b
, e
, and w
are input iterators.
The library defines three classes that implement different algorithms for generating random numbers. The library also defines three adaptors that modify the sequences produced by a given engine. The engine and engine adaptor classes are templates. Unlike the parameters to the distributions, the parameters to these engines are complex and require detailed understanding of the math used by the particular engine. We list the engines here so that the reader is aware of their existence, but describing how to generate these types is beyond the scope of this Primer.
The library also defines several types that are built from the engines or adaptors. The default_random_engine
type is a type alias for one of the engine types parameterized by variables designed to yield good performance for casual use. The library also defines several classes that are fully specialized versions of an engine or adaptor. The engines and the specializations defined by the library are:
default_random_engine
Type alias for one of the other engines intended to be used for most purposes.
linear_congruential_engine
minstd_rand0
Has a multiplier of 16807, a modulus of 2147483647, and an increment of 0.
minstd_rand
Has a multiplier of 48271, a modulus of 2147483647, and an increment of 0.
mersenne_twister_engine
mt19937
32-bit unsigned
Mersenne twister generator.
mt19937_64
64-bit unsigned
Mersenne twister generator.
subtract_with_carry_engine
ranlux24_base
32-bit unsigned
subtract with carry generator.
ranlux48_base
64-bit unsigned
subtract with carry generator.
discard_block_engine
Engine adaptor that discards results from its underlying engine. Parameterized by the underlying engine to use the block size, and size of the used blocks.
ranlux24
Uses the ranlux24_base
engine with a block size of 223 and a used block size of 23.
ranlux48
Uses the ranlux48_base
engine with a block size of 389 and a used block size of 11.
independent_bits_engine
Engine adaptor that generates numbers with a specified number of bits. Parameterized by the underlying engine to use, the number of bits to generate in its results, and an unsigned integral type to use to hold the generated bits. The number of bits specified must be less than the number of digits that the specified unsigned type can hold.
shuffle_order_engine
Engine adaptor that returns the same numbers as its underlying engine but delivers them in a different sequence. Parameterized by the underlying engine to use and the number of elements to shuffle.
knuth_b
Uses the minstd_rand0
engine with a table size of 256.