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