std::mersenne_twister_engine - cppreference.com
From cppreference.com
|
|
(since C++11) | |
mersenne_twister_engine is a random number engine based on Mersenne Twister algorithm. It produces high quality, but not cryptographically secure, unsigned integer random numbers of type UIntType on the interval [0, 2w
).
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 state |
| r | - | the number of bits of the lower bit-mask, also known as the twist value |
| a | - | the conditional xor-mask, i.e. the coefficients of the rational normal form twist matrix |
| u, d, s, b, t, c, l | - | the 1st to 7th components of the bit-scrambling (tempering) matrix |
| f | - | the initialization multiplier |
If any of the following restrictions is violated, the program is ill-formed:
mis in[1,n].- The following expressions are all
true:
w >= 3w >= rw >= uw >= sw >= tw >= lw <= std::numeric_limits<UIntType>::digits
- Given
(1u << w) - 1uasw1, the following expressions are alltrue:
a <= w1b <= w1c <= w1d <= w1f <= w1
Generator properties
The size of the states of mersenne_twister_engine is n, each of them consists of a sequence X of n values of type result_type. Xj stands for the j mod nth value (starting from 0) of X.
Given the following bitwise operation notations:
- bitand, built-in bitwise AND.
- xor, built-in bitwise XOR.
- lshift, built-in bitwise left-shift.
- rshift, built-in bitwise right-shift.
The transition algorithm of mersenne_twister_engine (TA(xi)) is defined as follows:
- Concatenate the upper
w - rbits of Xi-n with the lowerrbits of Xi+1-n to obtain an unsigned integer valueY. - Let
ybe a·(Y bitand 1), and set Xi to Xi+m−n xor (Y rshift 1) xor y.
The generation algorithm of mersenne_twister_engine (GA(xi)) is defined as follows:
- Let z1 be Xi xor ((Xi rshift u) bitand d).
- Let z2 be Xi xor (((Xi lshift s) mod 2w
) bitand b). - Let z3 be Xi xor (((Xi lshift t) mod 2w
) bitand c). - Let z4 be z3 xor (z3 rshift l).
- Deliver z4 as the result (i.e. GA(xi)=z4).
Predefined specializations
The following specializations define the random number engine with two commonly used parameter sets:
| Type | Definition |
mt19937 (C++11)
|
|
mt19937_64 (C++11)
|
|
Nested types
| Type | Definition |
result_type
|
UIntType
|
Data members
constexpr size_t word_size [static] |
w (public static member constant) |
constexpr size_t state_size [static] |
n (public static member constant) |
constexpr size_t shift_size [static] |
m (public static member constant) |
constexpr size_t mask_bits [static] |
r (public static member constant) |
constexpr UIntType xor_mask [static] |
a (public static member constant) |
constexpr size_t tempering_u [static] |
u (public static member constant) |
constexpr UIntType tempering_d [static] |
d (public static member constant) |
constexpr size_t tempering_s [static] |
s (public static member constant) |
constexpr UIntType tempering_b [static] |
b (public static member constant) |
constexpr size_t tempering_t [static] |
t (public static member constant) |
constexpr UIntType tempering_c [static] |
c (public static member constant) |
constexpr size_t tempering_l [static] |
l (public static member constant) |
constexpr UIntType initialization_multiplier [static] |
f (public static member constant) |
constexpr UIntType default_seed [static] |
5489u (public static member constant) |
Member functions
Construction and Seeding | |
| constructs the engine (public member function) [edit] | |
| sets the current state of the engine (public member function) [edit] | |
Generation | |
| advances the engine's state and returns the generated value (public member function) [edit] | |
| advances the engine's state by a specified amount (public member function) [edit] | |
Characteristics | |
[static] |
gets the smallest possible value in the output range (public static member function) [edit] |
[static] |
gets the largest possible value in the output range (public static member function) [edit] |