Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.

Class template mersenne_twister



// In header: <boost/random/mersenne_twister.hpp>

template<typename UIntType, int w, int n, int m, int r, UIntType a, int u, 
         int s, UIntType b, int t, UIntType c, int l, UIntType val> 
class mersenne_twister {
  // types
  typedef UIntType result_type;

  // construct/copy/destruct
  template<typename It> mersenne_twister(It &, It);
  template<typename Generator> mersenne_twister(Generator &);

  // public member functions
  void seed();
  void seed(UIntType);
  template<typename Generator> void seed(Generator &);
  template<typename It> void seed(It &, It);
  result_type min() const;
  result_type max() const;
  result_type operator()();

  // public static functions
  static bool validation(result_type);
  static const int word_size;
  static const int state_size;
  static const int shift_size;
  static const int mask_bits;
  static const UIntType parameter_a;
  static const int output_u;
  static const int output_s;
  static const UIntType output_b;
  static const int output_t;
  static const UIntType output_c;
  static const int output_l;
  static const bool has_fixed_range;


Instantiations of class template mersenne_twister model a pseudo-random number generator . It uses the algorithm described in

"Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator", Makoto Matsumoto and Takuji Nishimura, ACM Transactions on Modeling and Computer Simulation: Special Issue on Uniform Random Number Generation, Vol. 8, No. 1, January 1998, pp. 3-30.

[Note] Note

The boost variant has been implemented from scratch and does not derive from or use mt19937.c provided on the above WWW site. However, it was verified that both produce identical output.

The seeding from an integer was changed in April 2005 to address a weakness.

The quality of the generator crucially depends on the choice of the parameters. User code should employ one of the sensibly parameterized generators such as mt19937 instead.

The generator requires considerable amounts of memory for the storage of its state array. For example, mt11213b requires about 1408 bytes and mt19937 requires about 2496 bytes.

mersenne_twister public construct/copy/destruct

  1. mersenne_twister();

    Constructs a mersenne_twister and calls seed().

  2. mersenne_twister(UIntType value);

    Constructs a mersenne_twister and calls seed(value).

  3. template<typename It> mersenne_twister(It & first, It last);
  4. template<typename Generator> mersenne_twister(Generator & gen);

    Constructs a mersenne_twister and calls seed(gen).

    [Note] Note

    The copy constructor will always be preferred over the templated constructor.

mersenne_twister public member functions

  1. void seed();

    Calls seed(result_type(5489)).

  2. void seed(UIntType value);

    Sets the state x(0) to v mod 2w. Then, iteratively, sets x(i) to (i + 1812433253 * (x(i-1) xor (x(i-1) rshift w-2))) mod 2w for i = 1 .. n-1. x(n) is the first value to be returned by operator().

  3. template<typename Generator> void seed(Generator & gen);

    Sets the state of this mersenne_twister to the values returned by n invocations of gen.

    Complexity: Exactly n invocations of gen.

  4. template<typename It> void seed(It & first, It last);
  5. result_type min() const;
  6. result_type max() const;
  7. result_type operator()();

mersenne_twister public static functions

  1. static bool validation(result_type v);