##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Pseudo-random number generation

From cppreference.com
< cpp‎ | numeric

The random number library provides classes that generate random and pseudo-random numbers. These classes include:

- Random number engines (both pseudo-random number generators, which generate integer sequences with a uniform distribution, and true random number generators if available)
- Random number distributions (e.g. uniform, normal, or poisson distributions) which convert the output of random number engines into various statistical distributions

Engines and distributions are designed to be used together to produce random values. All of the engines may be specifically seeded, serialized, and deserialized for use with repeatable simulators.

### [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.

The choice of which engine to use involves a number of tradeoffs: the linear congruential engine is moderately fast and has a very small storage requirement for state. The lagged Fibonacci generators are very fast even on processors without advanced arithmetic instruction sets, at the expense of greater state storage and sometimes less desirable spectral characteristics. The Mersenne twister is slower and has greater state storage requirements but with the right parameters has the longest non-repeating sequence with the most desirable spectral characteristics (for a given definition of desirable).

Random number engines satisfy `UniformRandomBitGenerator`

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. They are generally used to alter the spectral characteristics of the underlying engine.

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` | std::linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647> Discovered in 1969 by Lewis, Goodman and Miller, adopted as "Minimal standard" in 1988 by Park and Miller [edit]
`minstd_rand` | std::linear_congruential_engine<std::uint_fast32_t, 48271, 0, 2147483647> Newer "Minimum standard", recommended by Park, Miller, and Stockmeyer in 1993[edit]
`mt19937` | std::mersenne_twister_engine<std::uint_fast32_t, 32, 624, 397, 31, 0x9908b0df, 11, 0xffffffff, 7, 0x9d2c5680, 15, 0xefc60000, 18, 1812433253> 32-bit Mersenne Twister by Matsumoto and Nishimura, 1998[edit]
`mt19937_64` | std::mersenne_twister_engine<std::uint_fast64_t, 64, 312, 156, 31, 0xb5026f5aa96619e9, 29, 0x5555555555555555, 17, 0x71d67fffeda60000, 37, 0xfff7eee000000000, 43, 6364136223846793005> 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000[edit]
`ranlux24_base` | std::subtract_with_carry_engine<std::uint_fast32_t, 24, 10, 24>[edit]
`ranlux48_base` | std::subtract_with_carry_engine<std::uint_fast64_t, 48, 5, 12>[edit]
`ranlux24` | std::discard_block_engine<std::ranlux24_base, 223, 23> 24-bit RANLUX generator by Martin Lüscher and Fred James, 1994[edit]
`ranlux48` | std::discard_block_engine<std::ranlux48_base, 389, 11> 48-bit RANLUX generator by Martin Lüscher and Fred James, 1994[edit]
`knuth_b` | std::shuffle_order_engine<std::minstd_rand0, 256>[edit]
`default_random_engine` | implementation-defined

#### [edit] Non-deterministic random numbers

std::random_device is a non-deterministic uniform random number generator, although implementations are allowed to implement std::random_device using a pseudo-random number engine if there is no support for non-deterministic random number generation.

random_device | non-deterministic random number generator using hardware entropy source (class) [edit]

### [edit] Random number distributions

A random number distribution post-processes the output of an random number engine in such a way that resulting output is distributed according to a defined statistical probability density function.

Random number distributions satisfy `RandomNumberDistribution`

Defined in header `<random>`
##### Uniform distributions
uniform_int_distribution (C++11) | produces integer values evenly distributed across a range (class template) [edit]
uniform_real_distribution (C++11) | produces real values evenly distributed across a range (class template) [edit]
##### 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] Utilities

Defined in header `<random>`
generate_canonical (C++11) | evenly distributes real values of given precision across [0, 1) (function template) [edit]
seed_seq (C++11) | general-purpose bias-eliminating scrambled seed sequence generator (class)

### [edit] C random library

In addition to the engines and distributions described above, the functions and constants from the C random library are also available though not recommended:

Defined in header `<cstdlib>`
rand | generates a pseudo-random number (function) [edit]
srand | seeds pseudo-random number generator (function) [edit]
RAND_MAX | maximum possible value generated by std::rand (macro constant) [edit]

### [edit] Example

Run this code

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <map>
    #include <random>
    #include <cmath>

    int main()
    {
        // Seed with a real random value, if available
        std::random_device r;

        // Choose a random mean between 1 and 6
        std::default_random_engine e1(r());
        std::uniform_int_distribution<int> uniform_dist(1, 6);
        int mean = uniform_dist(e1);
        std::cout << "Randomly-chosen mean: " << mean << '\n';

        // Generate a normal distribution around that mean
        std::seed_seq seed2{r(), r(), r(), r(), r(), r(), r(), r()};
        std::mt19937 e2(seed2);
        std::normal_distribution<> normal_dist(mean, 2);

        std::map<int, int> hist;
        for (int n = 0; n < 10000; ++n) {
            ++hist[std::round(normal_dist(e2))];
        }
        std::cout << "Normal distribution around " << mean << ":\n";
        for (auto p : hist) {
            std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                      << p.first << ' ' << std::string(p.second/200, '*') << '\n';
        }
    }

Possible output:

    Randomly-chosen mean: 4
    Normal distribution around 4:
    -4
    -3
    -2
    -1
     0 *
     1 ***
     2 ******
     3 ********
     4 *********
     5 ********
     6 ******
     7 ***
     8 *
     9
    10
    11
    12

### [edit] See also

C documentation for Pseudo-random number generation

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/random&oldid=86199"