Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRandom3.cxx
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: Peter Malzacher 31/08/99
3
4/**
5
6\class TRandom3
7
8Random number generator class based on
9 M. Matsumoto and T. Nishimura,
10 Mersenne Twister: A 623-dimensionally equidistributed
11 uniform pseudorandom number generator
12 ACM Transactions on Modeling and Computer Simulation,
13 Vol. 8, No. 1, January 1998, pp 3--30.
14
15For more information see the Mersenne Twister homepage
16 [http://www.math.keio.ac.jp/~matumoto/emt.html]
17
18Advantages:
19- large period (slightly less than 2**19937 -1)
20- relatively fast (slightly slower than TRandom2 but much faster than TRandom1)
21
22Drawbacks:
23- a relative large internal state of 624 integers
24- generate only 32 random bits
25- not passing all the random generator tests. It fails some tests in TestU01
26 (see [http://simul.iro.umontreal.ca/testu01/tu01.html])
27
28An alternatively excellent generator passing all tests of TestU01, having 61 random bits and
29being as fast as Mersenne and Twister is MIXMAX (TRandomMixMax).
30Also, TRandomRanluxpp is a recommended alternative over TRandom3.
31
32@warning TRandom3 is not a fully correct Mersenne and Twister random number generator, since
33zeroes of the sequence are skipped, and thus the actual period is slightly less than
342**19937 -1. Consider using instead std::mt19937. Other differences are that, unlike in the paper, 0 (skip)
35and 1 (here we divide by UINT_MAX + 1 instead of UINT_MAX - 1) are not included in the sequence.
36
37@ingroup Random
38
39*/
40
41//////////////////////////////////////////////////////////////////////
42// Aug.99 ROOT implementation based on CLHEP by P.Malzacher
43//
44// the original code contains the following copyright notice:
45/* This library is free software; you can redistribute it and/or */
46/* modify it under the terms of the GNU Library General Public */
47/* License as published by the Free Software Foundation; either */
48/* version 2 of the License, or (at your option) any later */
49/* version. */
50/* This library is distributed in the hope that it will be useful, */
51/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
52/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
53/* See the GNU Library General Public License for more details. */
54/* You should have received a copy of the GNU Library General */
55/* Public License along with this library; if not, write to the */
56/* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
57/* 02111-1307 USA */
58/* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
59/* When you use this, send an email to: matumoto@math.keio.ac.jp */
60/* with an appropriate reference to your work. */
61/////////////////////////////////////////////////////////////////////
62
63#include "TRandom3.h"
64#include "TBuffer.h"
65#include "TRandom2.h"
66#include "TUUID.h"
67
69#ifdef R__COMPLETE_MEM_TERMINATION
70namespace {
71 struct TRandomCleanup {
72 ~TRandomCleanup() { delete gRandom; gRandom = nullptr; }
73 };
75}
76#endif
77
78
79////////////////////////////////////////////////////////////////////////////////
80/// \brief Default constructor.
81///
82/// If seed is 0, the seed array is automatically computed via a TRandom2
83/// object, which internally uses TUUID.
84/// In this case the seed is guaranteed to be unique in space and time.
85
87{
88 SetName("Random3");
89 SetTitle("Random number generator: Mersenne Twister");
90 SetSeed(seed);
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// \brief Default destructor.
95
99
100////////////////////////////////////////////////////////////////////////////////
101/// \brief Machine independent random number generator.
102///
103/// Produces uniformly-distributed floating points in ]0, 1[.
104/// Method: Mersenne Twister
105/// Generate number in interval (0,1): 0 and 1 are not included in the interval
106
108{
109 UInt_t y;
110
111 const Int_t kM = 397;
112 const Int_t kN = 624;
113 const UInt_t kTemperingMaskB = 0x9d2c5680;
114 const UInt_t kTemperingMaskC = 0xefc60000;
115 const UInt_t kUpperMask = 0x80000000;
116 const UInt_t kLowerMask = 0x7fffffff;
117 const UInt_t kMatrixA = 0x9908b0df;
118
119 if (fCount624 >= kN) {
120 Int_t i;
121
122 for (i=0; i < kN-kM; i++) {
123 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
124 fMt[i] = fMt[i+kM] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
125 }
126
127 for ( ; i < kN-1 ; i++) {
128 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
129 fMt[i] = fMt[i+kM-kN] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
130 }
131
132 y = (fMt[kN-1] & kUpperMask) | (fMt[0] & kLowerMask);
133 fMt[kN-1] = fMt[kM-1] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
134 fCount624 = 0;
135 }
136
137 y = fMt[fCount624++];
138 y ^= (y >> 11);
139 y ^= ((y << 7 ) & kTemperingMaskB );
140 y ^= ((y << 15) & kTemperingMaskC );
141 y ^= (y >> 18);
142
143 // 2.3283064365386963e-10 == 1./(UINT_MAX+1UL) -> then returned value cannot be = 1.0
144 if (y) return ( (Double_t) y * 2.3283064365386963e-10); // * Power(2,-32)
145 return Rndm();
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// \brief Return an array of n random numbers uniformly distributed in ]0, 1[.
150
152{
153 for(Int_t i=0; i<n; i++) array[i]=(Float_t)Rndm();
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// \brief Return an array of n random numbers uniformly distributed in ]0, 1[.
158
160{
161 Int_t k = 0;
162
163 UInt_t y;
164
165 const Int_t kM = 397;
166 const Int_t kN = 624;
167 const UInt_t kTemperingMaskB = 0x9d2c5680;
168 const UInt_t kTemperingMaskC = 0xefc60000;
169 const UInt_t kUpperMask = 0x80000000;
170 const UInt_t kLowerMask = 0x7fffffff;
171 const UInt_t kMatrixA = 0x9908b0df;
172
173 while (k < n) {
174 if (fCount624 >= kN) {
175 Int_t i;
176
177 for (i=0; i < kN-kM; i++) {
178 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
179 fMt[i] = fMt[i+kM] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
180 }
181
182 for ( ; i < kN-1 ; i++) {
183 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
184 fMt[i] = fMt[i+kM-kN] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
185 }
186
187 y = (fMt[kN-1] & kUpperMask) | (fMt[0] & kLowerMask);
188 fMt[kN-1] = fMt[kM-1] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
189 fCount624 = 0;
190 }
191
192 y = fMt[fCount624++];
193 y ^= (y >> 11);
194 y ^= ((y << 7 ) & kTemperingMaskB );
195 y ^= ((y << 15) & kTemperingMaskC );
196 y ^= (y >> 18);
197
198 if (y) {
199 array[k] = Double_t( y * 2.3283064365386963e-10); // * Power(2,-32)
200 k++;
201 }
202 }
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// \brief Set the random generator sequence.
207///
208/// If seed is 0 (default value) a TRandom2 (internally uses TUUID) is used to
209/// generate all 624 unsigned integers of the seed array.
210/// In this case the seed is guaranteed to be unique in space and time.
211///
212/// Upgraded seeding procedure is used to fix a known problem when seeding with
213/// values with many zero in the bit pattern (like 2**28), see
214/// http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
215
217{
218 TRandom::SetSeed(seed);
219 fCount624 = 624;
220 if (seed > 0) {
221 fMt[0] = fSeed;
222
223 // use multipliers from Knuth's "Art of Computer Programming" Vol. 2, 3rd Ed. p.106
224 for(Int_t i=1; i<624; i++) {
225 fMt[i] = (1812433253 * ( fMt[i-1] ^ ( fMt[i-1] >> 30)) + i );
226 }
227
228 } else {
229
230 // use TRandom2 (which is based on TUUID to generate the seed.
231 // TRandom2 works fairly well and has been tested against example
232 // layout in https://savannah.cern.ch/bugs/?99516
233 TRandom2 r(0);
234 for (Int_t i = 0; i< 624; i++) {
235 fMt[i] = static_cast<UInt_t> (4294967296.*r.Rndm());
236 }
237 // warm up the generator calling it 10 times
238 for (Int_t i = 0; i < 10; ++i) Rndm();
239 }
240
241
242}
243
244////////////////////////////////////////////////////////////////////////////////
245/// \brief Streamer for an object of class TRandom3.
246
248{
249 if (R__b.IsReading()) {
251 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
252 if (R__v > 1) {
253 R__b.ReadClassBuffer(TRandom3::Class(), this, R__v, R__s, R__c);
254 return;
255 }
256 //====process old versions before automatic schema evolution
258 R__b.ReadStaticArray(fMt);
259 R__b >> fCount624;
260 R__b.CheckByteCount(R__s, R__c, TRandom3::IsA());
261 //====end of old versions
262
263 } else {
264 R__b.WriteClassBuffer(TRandom3::Class(),this);
265 }
266}
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
TRandom * gRandom
Definition TRandom3.cxx:68
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition TRandom2.h:27
Random number generator class based on M.
Definition TRandom3.h:27
UInt_t fMt[624]
Definition TRandom3.h:30
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom3.cxx:107
void RndmArray(Int_t n, Float_t *array) override
Return an array of n random numbers uniformly distributed in ]0, 1[.
Definition TRandom3.cxx:151
TClass * IsA() const override
Definition TRandom3.h:47
TRandom3(UInt_t seed=4357)
Default constructor.
Definition TRandom3.cxx:86
void Streamer(TBuffer &) override
Streamer for an object of class TRandom3.
Definition TRandom3.cxx:247
Int_t fCount624
Definition TRandom3.h:31
~TRandom3() override
Default destructor.
Definition TRandom3.cxx:96
void SetSeed(ULong_t seed=0) override
Set the random generator sequence.
Definition TRandom3.cxx:216
static TClass * Class()
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:614
UInt_t fSeed
Definition TRandom.h:30
void Streamer(TBuffer &) override
Stream an object of class TObject.
Double_t y[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16