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-diminsionally 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
18Advantage:
19
20- large period 2**19937 -1
21- relatively fast (slightly slower than TRandom2 but much faster than TRandom1)
22
23Drawback:
24- a relative large internal state of 624 integers
25- generate only 32 random bits
26- not passing all the random generator tests. It fails some tests in TestU01
27 (see [http://simul.iro.umontreal.ca/testu01/tu01.html])
28
29An altenativly excellent generator passing all tests of TestU01, having 61 random bits and
30fast as Mersenne and Twister is MIXMAX (TRandomMixMax).
31
32@ingroup Random
33
34*/
35
36//////////////////////////////////////////////////////////////////////
37// Aug.99 ROOT implementation based on CLHEP by P.Malzacher
38//
39// the original code contains the following copyright notice:
40/* This library is free software; you can redistribute it and/or */
41/* modify it under the terms of the GNU Library General Public */
42/* License as published by the Free Software Foundation; either */
43/* version 2 of the License, or (at your option) any later */
44/* version. */
45/* This library is distributed in the hope that it will be useful, */
46/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
47/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
48/* See the GNU Library General Public License for more details. */
49/* You should have received a copy of the GNU Library General */
50/* Public License along with this library; if not, write to the */
51/* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
52/* 02111-1307 USA */
53/* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
54/* When you use this, send an email to: matumoto@math.keio.ac.jp */
55/* with an appropriate reference to your work. */
56/////////////////////////////////////////////////////////////////////
57
58#include "TRandom3.h"
59#include "TBuffer.h"
60#include "TRandom2.h"
61#include "TUUID.h"
62
64#ifdef R__COMPLETE_MEM_TERMINATION
65namespace {
66 struct TRandomCleanup {
67 ~TRandomCleanup() { delete gRandom; gRandom = nullptr; }
68 };
69 static TRandomCleanup gCleanupRandom;
70}
71#endif
72
74
75////////////////////////////////////////////////////////////////////////////////
76/// Default constructor
77/// If seed is 0, the seed is automatically computed via a TUUID object.
78/// In this case the seed is guaranteed to be unique in space and time.
79
81{
82 SetName("Random3");
83 SetTitle("Random number generator: Mersenne Twister");
84 SetSeed(seed);
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Default destructor
89
91{
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// Machine independent random number generator.
96/// Produces uniformly-distributed floating points in (0,1)
97/// Method: Mersenne Twister
98
100{
101 UInt_t y;
102
103 const Int_t kM = 397;
104 const Int_t kN = 624;
105 const UInt_t kTemperingMaskB = 0x9d2c5680;
106 const UInt_t kTemperingMaskC = 0xefc60000;
107 const UInt_t kUpperMask = 0x80000000;
108 const UInt_t kLowerMask = 0x7fffffff;
109 const UInt_t kMatrixA = 0x9908b0df;
110
111 if (fCount624 >= kN) {
112 Int_t i;
113
114 for (i=0; i < kN-kM; i++) {
115 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
116 fMt[i] = fMt[i+kM] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
117 }
118
119 for ( ; i < kN-1 ; i++) {
120 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
121 fMt[i] = fMt[i+kM-kN] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
122 }
123
124 y = (fMt[kN-1] & kUpperMask) | (fMt[0] & kLowerMask);
125 fMt[kN-1] = fMt[kM-1] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
126 fCount624 = 0;
127 }
128
129 y = fMt[fCount624++];
130 y ^= (y >> 11);
131 y ^= ((y << 7 ) & kTemperingMaskB );
132 y ^= ((y << 15) & kTemperingMaskC );
133 y ^= (y >> 18);
134
135 // 2.3283064365386963e-10 == 1./(max<UINt_t>+1) -> then returned value cannot be = 1.0
136 if (y) return ( (Double_t) y * 2.3283064365386963e-10); // * Power(2,-32)
137 return Rndm();
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Return an array of n random numbers uniformly distributed in ]0,1]
142
144{
145 for(Int_t i=0; i<n; i++) array[i]=(Float_t)Rndm();
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Return an array of n random numbers uniformly distributed in ]0,1]
150
152{
153 Int_t k = 0;
154
155 UInt_t y;
156
157 const Int_t kM = 397;
158 const Int_t kN = 624;
159 const UInt_t kTemperingMaskB = 0x9d2c5680;
160 const UInt_t kTemperingMaskC = 0xefc60000;
161 const UInt_t kUpperMask = 0x80000000;
162 const UInt_t kLowerMask = 0x7fffffff;
163 const UInt_t kMatrixA = 0x9908b0df;
164
165 while (k < n) {
166 if (fCount624 >= kN) {
167 Int_t i;
168
169 for (i=0; i < kN-kM; i++) {
170 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
171 fMt[i] = fMt[i+kM] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
172 }
173
174 for ( ; i < kN-1 ; i++) {
175 y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
176 fMt[i] = fMt[i+kM-kN] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
177 }
178
179 y = (fMt[kN-1] & kUpperMask) | (fMt[0] & kLowerMask);
180 fMt[kN-1] = fMt[kM-1] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
181 fCount624 = 0;
182 }
183
184 y = fMt[fCount624++];
185 y ^= (y >> 11);
186 y ^= ((y << 7 ) & kTemperingMaskB );
187 y ^= ((y << 15) & kTemperingMaskC );
188 y ^= (y >> 18);
189
190 if (y) {
191 array[k] = Double_t( y * 2.3283064365386963e-10); // * Power(2,-32)
192 k++;
193 }
194 }
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Set the random generator sequence
199/// if seed is 0 (default value) a TUUID is generated and used to fill
200/// the first 8 integers of the seed array.
201/// In this case the seed is guaranteed to be unique in space and time.
202/// Use upgraded seeding procedure to fix a known problem when seeding with values
203/// with many zero in the bit pattern (like 2**28).
204/// see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
205
207{
208 TRandom::SetSeed(seed);
209 fCount624 = 624;
210 if (seed > 0) {
211 fMt[0] = fSeed;
212
213 // use multipliers from Knuth's "Art of Computer Programming" Vol. 2, 3rd Ed. p.106
214 for(Int_t i=1; i<624; i++) {
215 fMt[i] = (1812433253 * ( fMt[i-1] ^ ( fMt[i-1] >> 30)) + i );
216 }
217
218 } else {
219
220 // use TRandom2 (which is based on TUUId to generate the seed
221 // TRandom2 works fairly well and has been tested against example
222 // layout in https://savannah.cern.ch/bugs/?99516
223 TRandom2 r(0);
224 for (Int_t i = 0; i< 624; i++) {
225 fMt[i] = static_cast<UInt_t> (4294967296.*r.Rndm());
226 }
227 // warm up the generator calling it 10 times
228 for (Int_t i = 0; i < 10; ++i) Rndm();
229 }
230
231
232}
233
234////////////////////////////////////////////////////////////////////////////////
235/// Stream an object of class TRandom3.
236
238{
239 if (R__b.IsReading()) {
240 UInt_t R__s, R__c;
241 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
242 if (R__v > 1) {
243 R__b.ReadClassBuffer(TRandom3::Class(), this, R__v, R__s, R__c);
244 return;
245 }
246 //====process old versions before automatic schema evolution
247 TRandom::Streamer(R__b);
248 R__b.ReadStaticArray(fMt);
249 R__b >> fCount624;
250 R__b.CheckByteCount(R__s, R__c, TRandom3::IsA());
251 //====end of old versions
252
253 } else {
255 }
256}
short Version_t
Definition RtypesCore.h:65
unsigned long ULong_t
Definition RtypesCore.h:55
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
#define ClassImp(name)
Definition Rtypes.h:377
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:63
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=nullptr)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t ReadStaticArray(Bool_t *b)=0
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
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:99
void RndmArray(Int_t n, Float_t *array) override
Return an array of n random numbers uniformly distributed in ]0,1].
Definition TRandom3.cxx:143
TClass * IsA() const override
Definition TRandom3.h:46
TRandom3(UInt_t seed=4357)
Default constructor If seed is 0, the seed is automatically computed via a TUUID object.
Definition TRandom3.cxx:80
void Streamer(TBuffer &) override
Stream an object of class TRandom3.
Definition TRandom3.cxx:237
Int_t fCount624
Definition TRandom3.h:31
~TRandom3() override
Default destructor.
Definition TRandom3.cxx:90
void SetSeed(ULong_t seed=0) override
Set the random generator sequence if seed is 0 (default value) a TUUID is generated and used to fill ...
Definition TRandom3.cxx:206
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:608
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