Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRandom2.cxx
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: Rene Brun, Lorenzo Moneta 17/05/2006
3
4/**
5
6\class TRandom2
7
8Random number generator class based on the maximally equidistributed combined
9Tausworthe generator by L'Ecuyer.
10
11The period of the generator is 2**88 (about 10**26) and it uses only 3 words
12for the state.
13
14For more information see:
15P. L'Ecuyer, Mathematics of Computation, 65, 213 (1996)
16P. L'Ecuyer, Mathematics of Computation, 68, 225 (1999)
17
18The publications are available online at
19 [http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps]
20 [http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps]
21
22@ingroup Random
23
24*/
25
26#include "TRandom2.h"
27#include "TUUID.h"
28
29#define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) & 0xffffffffUL ) ^ ((((s <<a) & 0xffffffffUL )^s) >>b)
30
31
32
33////////////////////////////////////////////////////////////////////////////////
34/// Default constructor
35
37{
38 SetName("Random2");
39 SetTitle("Random number generator with period of about 10**26");
40 SetSeed(seed);
41}
42
43////////////////////////////////////////////////////////////////////////////////
44/// Default destructor
45
49
50////////////////////////////////////////////////////////////////////////////////
51/// TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers
52/// Use a mask of 0xffffffffUL to make in work on 64 bit machines
53/// Periodicity of about 10**26
54/// Generate number in interval (0,1): 0 and 1 are not included in the interval
55
57{
58 // scale by 1./(Max<UINT> + 1) = 1./4294967296
59 const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
60
61 UInt_t iy = operator()();
62 if (iy) return kScale * static_cast<Double_t>(iy);
63 return Rndm();
64}
65
66////////////////////////////////////////////////////////////////////////////////
67/// Return an array of n random numbers uniformly distributed in ]0, 1[
68
70{
71 const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
72
73 UInt_t iy;
74
75 for(Int_t i=0; i<n; i++) {
76 fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
77 fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
78 fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
79
80 iy = fSeed ^ fSeed1 ^ fSeed2;
81 if (iy) array[i] = (Float_t)(kScale*static_cast<Double_t>(iy));
82 else array[i] = Rndm();
83 }
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Return an array of n random numbers uniformly distributed in ]0, 1[
88
90{
91 const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
92
93 UInt_t iy;
94 for(Int_t i=0; i<n; i++) {
95 fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
96 fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
97 fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
98
99 iy = fSeed ^ fSeed1 ^ fSeed2;
100 if (iy) array[i] = kScale*static_cast<Double_t>(iy);
101 else array[i] = Rndm();
102 }
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// Set the generator seed.
107/// If the seed given is zero, generate automatically seed values which
108/// are different every time by using TUUID.
109/// If a seed is given generate the other two needed for the generator state using
110/// a linear congruential generator
111/// The only condition, stated at the end of the 1999 L'Ecuyer paper is that the seeds
112/// must be greater than 1,7 and 15.
113/// Note that after setting the seed the generator is warmed up by calling it internally few
114/// times therefore the returned seed in TRandom2::GetSeed will be a different value.
115/// For this generator the user will have to store the provided seed externally
116/// if he wants to reproduce the random sequence
118{
119#define LCG(n) ((69069 * n) & 0xffffffffUL) // linear congurential generator
120
121 if (seed > 0) {
122 fSeed = LCG (seed);
123 if (fSeed < 2) fSeed += 2UL;
124 fSeed1 = LCG (fSeed);
125 if (fSeed1 < 8) fSeed1 += 8UL;
126 fSeed2 = LCG (fSeed1);
127 if (fSeed2 < 16) fSeed2 += 16UL;
128 } else {
129 // initialize using a TUUID
130 TUUID u;
131 UChar_t uuid[16];
132 u.GetUUID(uuid);
133 fSeed = UInt_t(uuid[3])*16777216 + UInt_t(uuid[2])*65536 + UInt_t(uuid[1])*256 + UInt_t(uuid[0]);
134 fSeed1 = UInt_t(uuid[7])*16777216 + UInt_t(uuid[6])*65536 + UInt_t(uuid[5])*256 + UInt_t(uuid[4]);
135 fSeed2 = UInt_t(uuid[11])*16777216 + UInt_t(uuid[10])*65536 + UInt_t(uuid[9])*256 + UInt_t(uuid[8]);
136 // use also the other bytes
137 UInt_t seed3 = UInt_t(uuid[15])*16777216 + UInt_t(uuid[14])*65536 + UInt_t(uuid[13])*256 + UInt_t(uuid[12]);
138 fSeed2 += seed3;
139
140 if (fSeed < 2) fSeed += 2UL;
141 if (fSeed1 < 8) fSeed1 += 8UL;
142 if (fSeed2 < 16) fSeed2 += 16UL;
143 }
144
145 // "warm it up" by calling it 6 times
146 for (int i = 0; i < 6; ++i)
147 Rndm();
148
149 return;
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// \brief Returns one of the seeds of the generator.
154///
155/// \warning This is not the initial seed!
156///
157/// The internal state of the generator is described by three `UInt_t` numbers,
158/// called seed numbers, but they are not initial seeds. This function exposes
159/// one of them and can't provide full description of the generator state.
160///
162{
163 return fSeed;
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// \brief Return a random 32-bit integer, advancing the generator state by one step.
168///
169/// Implements the std::UniformRandomBitGenerator interface. Returns the raw
170/// Tausworthe XOR output directly, avoiding the round-trip through double.
171
173{
174 fSeed = TAUSWORTHE(fSeed, 13, 19, 4294967294UL, 12);
175 fSeed1 = TAUSWORTHE(fSeed1, 2, 25, 4294967288UL, 4);
176 fSeed2 = TAUSWORTHE(fSeed2, 3, 11, 4294967280UL, 17);
177 return fSeed ^ fSeed1 ^ fSeed2;
178}
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:70
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
const Float_t kScale
Definition TASImage.cxx:135
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define LCG(n)
#define TAUSWORTHE(s, a, b, c, d)
Definition TRandom2.cxx:29
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
~TRandom2() override
Default destructor.
Definition TRandom2.cxx:46
Double_t Rndm() override
TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers Use a mask of 0xffffffffUL to make ...
Definition TRandom2.cxx:56
TRandom2(UInt_t seed=1)
Default constructor.
Definition TRandom2.cxx:36
result_type operator()() override
Return a random 32-bit integer, advancing the generator state by one step.
Definition TRandom2.cxx:172
void RndmArray(Int_t n, Float_t *array) override
Return an array of n random numbers uniformly distributed in ]0, 1[.
Definition TRandom2.cxx:69
void SetSeed(ULong_t seed=0) override
Set the generator seed.
Definition TRandom2.cxx:117
UInt_t GetSeed() const override
Returns one of the seeds of the generator.
Definition TRandom2.cxx:161
UInt_t fSeed1
Definition TRandom2.h:30
UInt_t fSeed2
Definition TRandom2.h:31
UInt_t fSeed
Definition TRandom.h:31
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition TUUID.h:42
const Int_t n
Definition legend1.C:16