operator()
to produce values that are distributed in a manner that is defined by the distribution.mt19937
engine with uniform_int_distribution
, as shown in the code example later in this article.rand()
. For information about what's wrong with rand()
and how <random> addresses these shortcomings, see this video.random_device
. The second test run uses mersenne_twister_engine
as URNG, with a deterministic 32-bit constant seed, which means the results are repeatable. The third test run seeds mersenne_twister_engine
with a 32-bit non-deterministic result from random_device
. The fourth test run expands on this by using a seed sequence filled with random_device
results, which effectively gives more than 32-bit non-deterministic randomness (but still not crypto-secure). For more information, read on.random_device Class | Generates a non-deterministic, cryptographically secure random sequence by using an external device. Usually used to seed an engine. Low performance, very high quality. For more information, see Remarks. |
default_random_engine
The default engine.knuth_b
Knuth engine.minstd_rand0
1988 minimal standard engine (Lewis, Goodman, and Miller, 1969).minstd_rand
Updated minimal standard engine minstd_rand0
(Park, Miller, and Stockmeyer, 1993).mt19937
32-bit Mersenne twister engine (Matsumoto and Nishimura, 1998).mt19937_64
64-bit Mersenne twister engine (Matsumoto and Nishimura, 2000).ranlux24
24-bit RANLUX engine (Martin Lüscher and Fred James, 1994).ranlux24_base
Used as a base for ranlux24
.ranlux48
48-bit RANLUX engine (Martin Lüscher and Fred James, 1994).ranlux48_base
Used as a base for ranlux48
.linear_congruential_engine Class | Generates a random sequence by using the linear congruential algorithm. Most simplistic and lowest quality. |
mersenne_twister_engine Class | Generates a random sequence by using the Mersenne twister algorithm. Most complex, and is highest quality except for the random_device class. Very fast performance. |
subtract_with_carry_engine Class | Generates a random sequence by using the subtract-with-carry algorithm. An improvement on linear_congruential_engine , but much lower quality and performance than mersenne_twister_engine . |
discard_block_engine Class | Generates a random sequence by discarding values returned by its base engine. |
independent_bits_engine Class | Generates a random sequence with a specified number of bits by repacking bits from the values returned by its base engine. |
shuffle_order_engine Class | Generates a random sequence by reordering the values returned from its base engine. |
uniform_int_distribution Class | Produces a uniform integer value distribution across a range in the closed interval [a, b] (inclusive-inclusive). |
uniform_real_distribution Class | Produces a uniform real (floating-point) value distribution across a range in the half-open interval [a, b) (inclusive-exclusive). |
generate_canonical | Produces an even distribution of real (floating point) values of a given precision across [0, 1) (inclusive-exclusive). |
bernoulli_distribution Class | Produces a Bernoulli distribution of bool values. |
binomial_distribution Class | Produces a binomial distribution of integer values. |
geometric_distribution Class | Produces a geometric distribution of integer values. |
negative_binomial_distribution Class | Produces a negative binomial distribution of integer values. |
cauchy_distribution Class | Produces a Cauchy distribution of real (floating point) values. |
chi_squared_distribution Class | Produces a chi-squared distribution of real (floating point) values. |
fisher_f_distribution Class | Produces an F-distribution (also known as Snedecor's F distribution or the Fisher-Snedecor distribution) of real (floating point) values. |
lognormal_distribution Class | Produces a log-normal distribution of real (floating point) values. |
normal_distribution Class | Produces a normal (Gaussian) distribution of real (floating point) values. |
student_t_distribution Class | Produces a Student's t-distribution of real (floating point) values. |
exponential_distribution Class | Produces an exponential distribution of real (floating point) values. |
extreme_value_distribution Class | Produces an extreme value distribution of real (floating point) values. |
gamma_distribution Class | Produces a gamma distribution of real (floating point) values. |
poisson_distribution Class | Produces a Poisson distribution of integer values. |
weibull_distribution Class | Produces a Weibull distribution of real (floating point) values. |
discrete_distribution Class | Produces a discrete integer distribution. |
piecewise_constant_distribution Class | Produces a piecewise constant distribution of real (floating point) values. |
piecewise_linear_distribution Class | Produces a piecewise linear distribution of real (floating point) values. |
seed_seq Class | Generates a non-biased scrambled seed sequence. Used to avoid replication of random variate streams. Useful when many URNGs are instantiated from engines. |
operator | Tests whether the URNG on the left side of the operator is equal to the engine on the right side. |
operator!= | Tests whether the URNG on the left side of the operator is not equal to the engine on the right side. |
operator<< | Writes state information to a stream. |
operator>> | Extracts state information from a stream. |
IntType
indicates a short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long.UIntType
indicates unsigned short, unsigned int, unsigned long, or unsigned long long.RealType
indicates a float, double, or long double.typedef
numeric-type
result_type
is the type that is returned by the generator's operator()
. The numeric-type
is passed as a template parameter on instantiation.result_type operator()
returns values that are uniformly distributed between min()
and max()
.result_type min()
returns the minimum value that is returned by the generator's operator()
. Engine adaptors use the base engine's min()
result.result_type max()
returns the maximum value that is returned by the generator's operator()
. When result_type
is an integral (integer-valued) type, max()
is the maximum value that can actually be returned (inclusive); when result_type
is a floating-point (real-valued) type, max()
is the smallest value greater than all values that can be returned (non-inclusive). Engine adaptors use the base engine's max()
result.void seed(result_type s)
seeds the generator with seed value s
. For engines, the signature is void seed(result_type s = default_seed)
for default parameter support (engine adaptors define a separate void seed()
, see next subsection).template <class Seq> void seed(Seq& q)
seeds the generator by using a seed_seqSeq
.result_type x
that creates a generator seeded as if by calling seed(x)
.seed_seq& seq
that creates a generator seeded as if by calling seed(seq)
.void discard(unsigned long long count)
effectively calls operator()
count
times and discards each value.Engine
is the first template parameter of an engine adaptor, designating the base engine's type):const Engine& eng
. This is to support copy construction using the base engine.Engine&& eng
. This is to support move construction using the base engine.void seed()
that initializes the generator with the base engine's default seed value.const Engine& base()
property function that returns the base engine that was used to construct the generator.operator()
. The states of two generators instantiated from engines of the same type can be compared by using operator
and operator!=
. If the two states compare as equal, they will generate the same sequence of values. The state of an object can be saved to a stream as a sequence of 32-bit unsigned values by using the operator<<
of the generator. The state is not changed by saving it. A saved state can be read into generator instantiated from an engine of the same type by using operator>>
.typedef
numeric-type
result_type
is the type that is returned by the distribution's operator()
. The numeric-type
is passed as a template parameter on instantiation.template <class URNG> result_type operator()(URNG& gen)
returns values that are distributed according to the distribution's definition, by using gen
as a source of uniformly distributed random values and the stored parameters of the distribution.template <class URNG> result_type operator()(URNG& gen, param_type p)
returns values distributed in accordance with the distribution's definition, using gen
as a source of uniformly distributed random values and the parameters structure p
.typedef
unspecified-type
param_type
is the package of parameters optionally passed to operator()
and is used in place of the stored parameters to generate its return value.const param&
constructor initializes the stored parameters from its argument.param_type param() const
gets the stored parameters.void param(const param_type&)
sets the stored parameters from its argument.result_type min()
returns the minimum value that is returned by the distribution's operator()
.result_type max()
returns the maximum value that is returned by the distribution's operator()
. When result_type
is an integral (integer-valued) type, max()
is the maximum value that can actually be returned (inclusive); when result_type
is a floating-point (real-valued) type, max()
is the smallest value greater than all values that can be returned (non-inclusive).void reset()
discards any cached values, so that the result of the next call to operator()
does not depend on any values obtained from the engine before the call.typedef
distribution-type
distribution_type
, which is the type of its distribution.mt19937
and random_device
—as shown in this comparison table:URNG | Fast | Crypto-secure | Seedable | Deterministic |
---|---|---|---|---|
mt19937 | Yes | No | Yes | Yes* |
random_device | No | Yes | No | No |
random_device
to be cryptographically secure, in Visual Studio it is implemented to be cryptographically secure. (The term 'cryptographically secure' does not imply guarantees, but refers to a minimum level of entropy—and therefore, the level of predictability—a given randomization algorithm provides. For more information, see the Wikipedia article Cryptographically secure pseudorandom number generator.) Because the ISO C++ Standard does not require this, other platforms may implement random_device
as a simple pseudo-random number generator (not cryptographically secure) and may only be suitable as a seed source for another generator. Check the documentation for those platforms when using random_device
in cross-platform code.random_device
results are not reproducible, and a side-effect is that it may run significantly slower than other URNGs. Most applications that are not required to be cryptographically secure use mt19937
or a similar engine, although you may want to seed it with a call to random_device
, as shown in the code example.