std::mersenne_twister_engine
<random>
class UIntType, std::size_t w, std::size_t n, std::size_t m, std::size_t r,
UIntType a, std::size_t u, UIntType d, std::size_t s,
UIntType b, std::size_t t, UIntType c, std::size_t l, UIntType f
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 \(\scriptsize {[0,2^w)}\)[0, 2w
).
Contents
[edit] Template parameters
If any of the following restrictions is violated, the program is ill-formed:
- m is in
[
1,
n]
. - The following expressions are all true:
- w >= 3
- w >= r
- w >= u
- w >= s
- w >= t
- w >= l
- w <= std::numeric_limits <UIntType>::digits
- Given (1u << w) - 1u as w1, the following expressions are all true:
- a <= w1
- b <= w1
- c <= w1
- d <= w1
- f <= w1
[edit] 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
. \(\scriptsize X_j\)Xj stands for the \(\scriptsize j\mod n\)j mod nth value (starting from 0) of X.
Given the following bitwise operation notations:
- \(\scriptsize \mathsf{bitand}\)bitand, built-in bitwise AND.
- \(\scriptsize \mathsf{xor}\)xor, built-in bitwise XOR.
- \(\scriptsize \mathsf{lshift}\)lshift, built-in bitwise left-shift.
- \(\scriptsize \mathsf{rshift}\)rshift, built-in bitwise right-shift.
The transition algorithm of mersenne_twister_engine
(\(\scriptsize TA(x_i)\)TA(xi)) is defined as follows:
- Concatenate the upper w - r bits of \(\scriptsize X_{i-n}\)Xi-n with the lower r bits of \(\scriptsize X_{i+1-n}\)Xi+1-n to obtain an unsigned integer value Y.
- Let y be \(\scriptsize a \cdot (Y\ \mathsf{bitand}\ 1)\)a·(Y bitand 1), and set \(\scriptsize X_i\)Xi to \(\scriptsize X_{i+m−n}\ \mathsf{xor}\ (Y\ \mathsf{rshift}\ 1)\ \mathsf{xor}\ y\)Xi+m−n xor (Y rshift 1) xor y.
The generation algorithm of mersenne_twister_engine
(\(\scriptsize GA(x_i)\)GA(xi)) is defined as follows:
- Let \(\scriptsize z_1\)z1 be \(\scriptsize X_i\ \mathsf{xor}\ ((X_i\ \mathsf{rshift}\ u)\ \mathsf{bitand}\ d)\)Xi xor ((Xi rshift u) bitand d).
- Let \(\scriptsize z_2\)z2 be \(\scriptsize z_1\ \mathsf{xor}\ (((z_1\ \mathsf{lshift}\ s)\mod 2^w)\ \mathsf{bitand}\ b)\)Xi xor (((Xi lshift s) mod 2w
) bitand b). - Let \(\scriptsize z_3\)z3 be \(\scriptsize z_2\ \mathsf{xor}\ (((z_2\ \mathsf{lshift}\ t)\mod 2^w)\ \mathsf{bitand}\ c)\)Xi xor (((Xi lshift t) mod 2w
) bitand c). - Let \(\scriptsize z_4\)z4 be \(\scriptsize z_3\ \mathsf{xor}\ (z_3\ \mathsf{rshift}\ l)\)z3 xor (z3 rshift l).
- Deliver \(\scriptsize z_4\)z4 as the result (i.e. \(\scriptsize GA(x_i)=z_4\)GA(xi)=z4).
[edit] Predefined specializations
The following specializations define the random number engine with two commonly used parameter sets:
<random>
mt19937
(C++11)
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
(C++11)
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]
[edit] Nested types
result_type
UIntType
[edit] Data members
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
(public static member constant)
[edit] Member functions
Construction and Seeding
Generation
Characteristics
(public static member function) [edit]
[edit] Non-member functions
(function) [edit]
[edit] Example
Reason: no example