std::mersenne_twister_engine
|
Defined in header
<random> |
||
|
template<
class UIntType, |
(since C++11) | |
mersenne_twister_engine is a random number engine based on Mersenne Twister algorithm. It produces high quality unsigned integer random numbers of type UIntType on the interval [0, 2w
).
The following type aliases define the random number engine with two commonly used parameter sets:
|
Defined in header
<random> |
|
| Type | Definition |
mt19937(C++11) |
std::mersenne_twister_engine<std::uint_fast32_t, 32, 624, 397, 31, |
mt19937_64(C++11) |
std::mersenne_twister_engine<std::uint_fast64_t, 64, 312, 156, 31, |
Template parameters
| UIntType | - | The result type generated by the generator. The effect is undefined if this is not one of unsigned short, unsigned int, unsigned long, or unsigned long long. |
| w | - | the power of two that determines the range of values generated by the engine |
| n | - | the degree of recurrence |
| m | - | the middle word, an offset used in the recurrence relation defining the series x, 1 ≤ m < n |
| r | - | the number of bits of the lower bit-mask, 0 ≤ r ≤ w - 1, also known as the twist value |
| a | - | the conditional xor-mask, i.e. the coefficients of the rational normal form twist matrix |
| u | - | 1st component of the bit-scrambling (tempering) matrix |
| d | - | 2nd component of the bit-scrambling (tempering) matrix |
| s | - | 3rd component of the bit-scrambling (tempering) matrix |
| b | - | 4th component of the bit-scrambling (tempering) matrix |
| t | - | 5th component of the bit-scrambling (tempering) matrix |
| c | - | 6th component of the bit-scrambling (tempering) matrix |
| l | - | 7th component of the bit-scrambling (tempering) matrix |
| f | - | the initialization multiplier |
The following relations shall hold:
2 < w,
r <= w,
u <= w,
s <= w,
t <= w,
l <= w,
w <= std::numeric_limits<UIntType>::digits,
a <= 2^w-1,
b <= 2^w-1,
c <= 2^w-1,
d <= 2^w-1, and
Member types
| Member type | Definition |
result_type(C++11) |
The integral type, UIntType, generated by the engine. Results are undefined if this is not an unsigned integral type. |
Member functions
Construction and Seeding |
|
|
(C++11)
|
constructs the engine (public member function) |
|
(C++11)
|
sets the current state of the engine (public member function) |
Generation |
|
|
(C++11)
|
advances the engine's state and returns the generated value (public member function) |
|
(C++11)
|
advances the engine's state by a specified amount (public member function) |
Characteristics |
|
|
[static] (C++11)
|
gets the smallest possible value in the output range (public static member function) |
|
[static] (C++11)
|
gets the largest possible value in the output range (public static member function) |
Non-member functions
|
(C++11)(C++11)(removed in C++20)
|
compares the internal states of two pseudo-random number engines (function) |
|
(C++11)
|
performs stream input and output on pseudo-random number engine (function template) |
Member constants
|
constexpr size_t word_size
[static] (C++11)
|
the template parameter w, determines the range of values generated by the engine.(public static member constant) |
|
constexpr size_t state_size
[static] (C++11)
|
the template parameter n. The engine state is n values of UIntType(public static member constant) |
|
constexpr size_t shift_size
[static] (C++11)
|
the template parameter m(public static member constant) |
|
constexpr size_t mask_bits
[static] (C++11)
|
the template parameter r, also known as the twist value.(public static member constant) |
|
constexpr UIntType xor_mask
[static] (C++11)
|
the template parameter a, the conditional xor-mask.(public static member constant) |
|
constexpr size_t tempering_u
[static] (C++11)
|
the template parameter u, first component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr UIntType tempering_d
[static] (C++11)
|
the template parameter d, second component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr size_t tempering_s
[static] (C++11)
|
the template parameter s, third component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr UIntType tempering_b
[static] (C++11)
|
the template parameter b, fourth component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr size_t tempering_t
[static] (C++11)
|
the template parameter t, fifth component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr UIntType tempering_c
[static] (C++11)
|
the template parameter c, sixth component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr size_t tempering_l
[static] (C++11)
|
the template parameter l, seventh component of the bit-scrambling (tempering) matrix(public static member constant) |
|
constexpr UIntType initialization_multiplier
[static] (C++11)
|
the template parameter f(public static member constant) |
|
constexpr UIntType default_seed
[static] (C++11)
|
the constant value 5489u (public static member constant) |
Notes
The Nth consecutive invocation of a default-constructed engine is required to produce the following value:
N |
The rangom engine type | The value to produce |
|---|---|---|
10000 |
std::mt19937 |
4123659995 |
10000 |
std::mt19937_64 |
9981545732273789042 |
#include <random> #include <cassert> int main() { std::mt19937 gen32; std::mt19937_64 gen64; for (auto n{1}; n != 10'000; gen32(), gen64(), ++n); assert(gen32() == 4'123'659'995 and gen64() == 9'981'545'732'273'789'042ULL); }