Export (0) Print
Expand All

mersenne_twister_engine Class

Generates a random sequence by the Mersenne twister algorithm.

template<class UIntType,
   size_t W, size_t N, size_t M, size_t R,
   UIntType A, size_t U,
   UIntType D, size_t S,
   UIntType B, size_t T,
   UIntType C, size_t L
   UIntType F>
class mersenne_twister_engine {
public:
   typedef UIntType result_type;
   static const size_t word_size = W;
   static const size_t state_size = N;
   static const size_t shift_size = M;
   static const size_t mask_bits = R;
   static const UIntType xor_mask = A;
   static const size_t tempering_u = U;
   static const size_t tempering_d = D;
   static const size_t tempering_s = S;
   static const UIntType tempering_b = B;
   static const size_t tempering_t = T;
   static const UIntType tempering_c = C;
   static const size_t tempering_l = L;
   static const UIntType initialization_multiplier = C;
   static const result_type default_seed = 5489U;
   explicit mersenne_twister_engine(unsigned long x0 = default_seed);
   explicit mersenne_twister_engine(seed_seq& seq);
   void seed(unsigned long x0 = default_seed);
   template<class Gen>
      void seed(seed_seq& seq);
   result_type min() const;
   result_type max() const;
   result_type operator()();
   void discard(unsigned long long count)();
   };

UIntType

The unsigned integer result type.

W

The W engine parameter.

N

The N engine parameter.

M

The M engine parameter.

R

The R engine parameter.

A

The A engine parameter.

U

The U engine parameter.

D

The D engine parameter.

S

The S engine parameter.

B

The B engine parameter.

T

The T engine parameter.

C

The C engine parameter.

L

The L engine parameter.

F

The F engine parameter.

This template class decribes a <random>. It holds a large integral value with W * (N - 1) + R bits. It extracts W bits at a time from this large value, and when it has used all the bits it twists the large value by shifting and mixing the bits so that it has a new set of bits to extract from. The engine's state is the last N W-bit values used if operator() has been called at least N times, otherwise the M W-bit values that have been used and the last N - M values of the seed.

The template argument UIntType must be large enough to hold values up to 2W - 1. The values of the other template arguments must satisfy the following requirements:

1 < M <= N

0 <= R, U, S, T, L <= W

0 <= A, B, C <= 2W - 1

W * (N - 1) + R must be a Mersenne prime

The generator twists the large value that it holds by executing the following code:

    for (size_t i = 0; i < N; ++i)
        {    // twist
        temp = (x[i] & LMASK) << (W - 1) | (x[i + 1] & HMASK) >> 1;
        if (temp & 1)
            y[i] = (temp >> 1) ^ A ^ x[(i + R) % N];
        else
            y[i] = (temp >> 1) ^ x[(i + R) % N];
        }
        for (size_t i = 0; i < N; ++i)
            x[i] = y[i];

where LMASK is an unsigned W-bit value with its low R bits set to 1 and the rest of its bits set to 0, and HMASK is the complement of LMASK.

The generator holds a current index idx initialized to 0. It extracts bits by executing the following code:

    temp = x[idx++];
    temp = temp ^ ((temp >> U) & D);
    temp = temp ^ ((temp << S) & B);
    temp = temp ^ ((temp << T) & C);
    temp = temp ^ (temp >> L);

When idx reaches N the generator twists the stored value and sets idx back to 0.

Header: <random>

Namespace: std

Community Additions

ADD
Show:
© 2015 Microsoft