C++ Reference ยท Memory and Utilities
C++ random engines: mt19937 and friends
Reference for C++ random number engines: mt19937, mt19937_64, minstd_rand, ranlux, default_random_engine, seeding with seed_seq, discard and state.
A random number engine is a function object that produces an endless stream of unsigned integers from an internal state. Calling engine() returns the next number and advances the state. Given the same seed, an engine always produces exactly the same sequence, which is what makes simulations and tests reproducible.
The standard offers several engines. std::mt19937 (Mersenne Twister, 32-bit, period 2^19937-1) is the usual general-purpose choice; std::mt19937_64 is its 64-bit sibling. std::minstd_rand0 and std::minstd_rand are tiny linear congruential generators. std::ranlux24 and std::ranlux48 are slow but high quality. std::knuth_b shuffles the output of a simpler engine. std::default_random_engine is whatever the implementation picks.
std::mt19937 gen{42}; std::uint32_t first = gen();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
std::mt19937 gen; // default seed (5489) std::mt19937 gen{seedValue}; // from an integer std::mt19937 gen{seedSeq}; // from a std::seed_seq result_type r = gen(); // next value gen.seed(newSeed); gen.discard(n);
| Engine | Description |
|---|---|
mt19937 |
32-bit Mersenne Twister; result_type is uint_fast32_t; good quality, 624 words of state (roughly 2.5 to 5 KB). |
mt19937_64 |
64-bit variant. |
minstd_rand0, minstd_rand |
Linear congruential engines with 31-bit output; tiny state, weak statistical quality. |
ranlux24, ranlux48 |
Subtract-with-carry engines with discarded output; high quality, slow. |
knuth_b |
Shuffle-order engine built on minstd_rand0. |
default_random_engine |
Implementation-defined choice. |
| Member | Description |
|---|---|
operator() |
Returns the next value. |
seed(...) |
Reinitialize the state. |
discard(n) |
Advance as if operator() were called n times. |
min(), max() |
Smallest and largest possible results (static). |
==, !=, <<, >> |
Compare engines; save and restore state through streams. |
Returns: the next unsigned integer in [min(), max()].
Errors / undefined behavior: engines have no failure mode. Using one engine from several threads without a lock is a data race.
Usage notes
- Reproducible sequences. The standard fixes the algorithm and the results for each named engine:
std::mt19937 gen{1}yields the same numbers on every conforming implementation. The 10000th value of a default-constructedmt19937is 4123659995. - Seeding quality. An integer seed initializes the whole state through a recurrence, which is fine for ordinary programs. When many streams must be uncorrelated, seed from a
std::seed_seqor from severalrandom_deviceoutputs. - Do not use
time(nullptr)as the only seed if programs can start in the same second. - Engines are cheap to copy only when small.
mt19937holds 624 words of state (about 5 KB in libstdc++); pass by reference and avoid copying it into every function. - Saving state: write the engine to a stream with
<<and read it back with>>to resume a stream later or send it between runs. - Thread safety: use
thread_localengines or one engine per thread, as shown. Seeding each from a different integer (orseed_seq) gives different streams. - Not cryptographic. All of these engines are predictable after observing enough output.
- Choose a family by need:
mt19937for general work,mt19937_64for 64-bit values,minstd_randfor tiny state,ranlux48for quality when speed does not matter. std::default_random_enginevaries between standard libraries, so avoid it if outputs must match across compilers.