Logo ROOT   6.08/07
Reference Guide
MersenneTwisterEngine.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Tue Aug 4 2015
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2015 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // random engines based on ROOT
12 
13 #ifndef ROOT_Math_MersenneTwisterEngine
14 #define ROOT_Math_MersenneTwisterEngine
15 
16 #ifndef ROOT_Math_TRandomEngine
17 #include "Math/TRandomEngine.h"
18 #endif
19 
20 #include <cstdint>
21 #include <vector>
22 #include <string>
23 
24 namespace ROOT {
25 
26  namespace Math {
27 
28  /**
29  Random number generator class based on
30  M. Matsumoto and T. Nishimura,
31  Mersenne Twister: A 623-diminsionally equidistributed
32  uniform pseudorandom number generator
33  ACM Transactions on Modeling and Computer Simulation,
34  Vol. 8, No. 1, January 1998, pp 3--30.
35 
36  For more information see the Mersenne Twister homepage
37  [http://www.math.keio.ac.jp/~matumoto/emt.html]
38 
39  Advantage:
40 
41  - large period 2**19937 -1
42  - relativly fast (slightly slower than TRandom1 and TRandom2 but much faster than TRandom1)
43 
44  Note that this is a 32 bit implementation. Only 32 bits of the returned double numbers are random.
45  in case more precision is needed, one should use an engine providing at least 48 random bits.
46 
47  Drawback: a relative large internal state of 624 integers
48 
49  @ingroup Random
50  */
51 
53 
54 
55  public:
56 
58  typedef uint32_t Result_t;
59  typedef uint32_t StateInt_t;
60 
61 
62  MersenneTwisterEngine(uint32_t seed=4357) {
63  SetSeed(seed);
64  }
65 
67 
68  void SetSeed(Result_t seed);
69 
70  virtual double Rndm() {
71  return Rndm_impl();
72  }
73  inline double operator() () { return Rndm_impl(); }
74 
75  uint32_t IntRndm() {
76  return IntRndm_impl();
77  }
78 
79  /// minimum integer taht can be generated
80  static unsigned int MinInt() { return 0; }
81  /// maximum integer taht can be generated
82  static unsigned int MaxInt() { return 0xffffffff; } // 2^32 -1
83 
84  static int Size() { return kSize; }
85 
86  static std::string Name() {
87  return "MersenneTwisterEngine";
88  }
89 
90  protected:
91  // functions used for testing
92 
93  void SetState(const std::vector<uint32_t> & state) {
94  for (unsigned int i = 0; i < kSize; ++i)
95  fMt[i] = state[i];
96  fCount624 = kSize; // to make sure we re-iterate on the new state
97  }
98 
99  void GetState(std::vector<uint32_t> & state) {
100  state.resize(kSize);
101  for (unsigned int i = 0; i < kSize; ++i)
102  state[i] = fMt[i];
103  }
104 
105  int Counter() const { return fCount624; }
106 
107 
108  private:
109 
110  double Rndm_impl();
111  uint32_t IntRndm_impl();
112 
113  enum {
114  kSize=624
115  };
116  uint32_t fMt[kSize];
118  };
119 
120 
121  } // end namespace Math
122 
123 } // end namespace ROOT
124 
125 
126 #endif /* ROOT_Math_TRandomEngines */
static unsigned int MaxInt()
maximum integer taht can be generated
void GetState(std::vector< uint32_t > &state)
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
Random number generator class based on M.
static unsigned int MinInt()
minimum integer taht can be generated
void SetSeed(Result_t seed)
set the seed x
double Rndm_impl()
generate a random double number
void SetState(const std::vector< uint32_t > &state)
Namespace for new Math classes and functions.