Condividi tramite


mersenne_twister Class

Generates a random sequence by the Mersenne twister algorithm. Retained for TR1 compatibility. Use mersenne_twister_engine Class instead.

template<class UIntType,
   int W, int N, int M, int R,
   UIntType A, int U, int S,
   UIntType B, int T, UIntType C, int L>
   class mersenne_twister {
public:
   typedef mersenne_twister<UIntType, W, N, M, R,
      P, U, S, B, T, C, L> _MyT;
   typedef UIntType result_type;
   static const int word_size = W;
   static const int state_size = N;
   static const int shift_size = M;
   static const int mask_bits = R;
   static const int UIntType parameter_a = A;
   static const int output_u = U;
   static const int output_s = S;
   static const UIntType output_b = B;
   static const int output_t = T;
   static const UIntType output_c = C;
   static const int output_l = L;
   static const UIntType default_seed = 5489U;
   explicit mersenne_twister(unsigned long x0 = default_seed);
   mersenne_twister(const mersenne_twister& right);
   mersenne_twister(mersenne_twister& right);
   template<class Gen>
      mersenne_twister(Gen& gen);
   void seed(unsigned long x0 = default_seed);
   template<class Gen>
      void seed(Gen& gen);
   result_type min() const;
   result_type max() const;
   result_type operator()();
   };

Parameters

  • UIntType
    The unsigned integer result type.

  • W
    The W engine parameter.

  • N
    The N engine parameter.

  • R
    The R engine parameter.

  • A
    The A engine parameter.

  • U
    The U 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.

Remarks

The template class decribes a simple engine. 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:

0 < M <= N

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

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

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

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

    for (int i = 0; i < N; ++i)
        {
        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 (int 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);
    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.

Requirements

Header: <random>

Namespace: std

See Also

Reference

<random>

mersenne_twister::mersenne_twister

mersenne_twister::operator()

mersenne_twister::seed

Other Resources

<random> Members