Pseudo-random number generation

From Cppreference

Jump to: navigation, search

Contents

[edit] Random number engines

Random number engines generate pseudo-random numbers using seed data as entropy source. Several different classes of pseudo-random number generation algorithms are implemented as templates that can be customized.

Defined in header <random>
linear_congruential_engine (C++11)
implements linear congruential algorithm
(class template)
mersenne_twister_engine (C++11)
implements Mersenne twister algorithm
(class template)
subtract_with_carry_engine (C++11)
implements subtract with carry (a lagged Fibonacci) algorithm
(class template)

[edit] Random number engine adaptors

Random number engine adaptors generate pseudo-random numbers using another random number engine as entropy source.

Defined in header <random>
discard_block_engine (C++11)
discards some output of a random number engine
(class template)
independent_bits_engine (C++11)
packs the output of a random number engine into blocks of specified number of bits
(class template)
shuffle_order_engine (C++11)
delivers the output of a random number engine in different order
(class template)

[edit] Predefined random number generators

Several specific popular algorithms are predefined.

Defined in header <random>
Type Definition
minstd_rand0 linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
minstd_rand linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
mt19937 mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31,

                        0x9908b0df, 11,
                        0xffffffff, 7,
                        0x9d2c5680, 15,
                        0xefc60000, 18, 1812433253>

mt19937_64 mersenne_twister_engine<uint_fast64_t, 64, 312, 156, 31,

                        0xb5026f5aa96619e9, 29,
                        0x5555555555555555, 17,
                        0x71d67fffeda60000, 37,
                        0xfff7eee000000000, 43, 6364136223846793005>

ranlux24_base subtract_with_carry_engine<uint_fast32_t, 24, 10, 24>
ranlux48_base subtract_with_carry_engine<uint_fast64_t, 48, 5, 12>
ranlux24 discard_block_engine<ranlux24_base, 223, 23>
ranlux48 discard_block_engine<ranlux48_base, 389, 11>
knuth_b shuffle_order_engine<minstd_rand0, 256>
default_random_engine implementation-defined
random_device
non-deterministic random number generator using hardware entropy source
(class)

[edit] Random number distributions

Random number distributions are post-process the output of an random number engine or adaptors in such a way, that resulting output fits into some statistical probability distribution.

Defined in header <random>
Uniform distributions
uniform_int_distribution (C++11)
evenly distributes integer random numbers across a range
(class template)
uniform_real_distribution (C++11)
evenly distributes floating-point random numbers across a range
(class template)
generate_canonical (C++11)
evenly distributes floating-point random numbers of given precision across [0, 1)
(function template)
Bernoulli distributions
bernoulli_distribution (C++11)
Produces bool values on a Bernoulli distribution.
(class)
binomial_distribution (C++11)
Produces integer values on a binomial distribution.
(class template)
negative_binomial_distribution (C++11)
Produces integer values on a negative binomial distribution.
(class template)
geometric_distribution (C++11)
Produces integer values on a geometric distribution.
(class template)
Poisson distributions
poisson_distribution (C++11)
Produces integer values on a poisson distribution.
(class template)
exponential_distribution (C++11)
Produces real values on an exponential distribution.
(class template)
gamma_distribution (C++11)
Produces real values on an gamma distribution.
(class template)
weibull_distribution (C++11)
Produces real values on a Weibull distribution.
(class template)
extreme_value_distribution (C++11)
Produces real values on an extreme value distribution.
(class template)
Normal distributions
normal_distribution (C++11)
Produces real values on a standard normal (Gaussian) distribution.
(class template)
lognormal_distribution (C++11)
Produces real values on a lognormal distribution.
(class template)
chi_squared_distribution (C++11)
Produces real values on a chi-squared distribution.
(class template)
cauchy_distribution (C++11)
Produces real values on a Cauchy distribution.
(class template)
fisher_f_distribution (C++11)
Produces real values on a Fisher's F-distribution.
(class template)
student_t_distribution (C++11)
Produces real values on a Student's t-distribution.
(class template)
Sampling distributions
discrete_distribution (C++11)
Produces random integers on a discrete distribution.
(class template)
piecewise_constant_distribution (C++11)
Produces real values distributed on constant subintervals.
(class template)
piecewise_linear_distribution (C++11)
Produces real values distributed on defined subintervals.
(class template)

[edit] Predefined seed sequence generators

Defined in header <random>
seed_seq (C++11)
general-purpose bias-eliminating scrambled seed sequence generator
(class)

[edit] C library

Defined in header <cstdlib>
rand
generates a pseudo-random number
(function)
srand
initializes pseudo-random number generator
(function)
RAND_MAX
maximum possible value generated by rand()
(macro constant)