W3cubDocs

/C++

std::mersenne_twister_engine

Defined in header <random>
template<
    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
> class mersenne_twister_engine;
(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 \(\scriptsize {[0,2^w)}\)[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,
0x9908b0df, 11,
0xffffffff, 7,
0x9d2c5680, 15,
0xefc60000, 18, 1812433253>

32-bit Mersenne Twister by Matsumoto and Nishimura, 1998.

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.

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:

  • 0 < m ≤ n
  • 2 < w
  • r ≤ w
  • u ≤ w
  • s ≤ w
  • t ≤ w
  • l ≤ w
  • w ≤ std::numeric_limits<UIntType>::digits
  • a ≤ 2w-1
  • b ≤ 2w-1
  • c ≤ 2w-1
  • d ≤ 2w-1
  • f ≤ 2w-1

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 random engine type The value to produce
10000 std::mt19937 4123659995
10000 std::mt19937_64 9981545732273789042

This is to guarantee that the random engine is conforming to the standard (see N1398).

#include <cassert>
#include <random>
 
int main()
{
    std::mt19937 gen32;
    std::mt19937_64 gen64;
 
    gen32.discard(10'000 - 1);
    gen64.discard(10'000 - 1);
 
    assert(gen32() == 4'123'659'995);
    assert(gen64() == 9'981'545'732'273'789'042ull);
}

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine