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 quidistributed 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
30
31////////////////////////////////////////////////////////////////////////////////
32/// Default constructor
33
35{
36 SetName("Random2");
37 SetTitle("Random number generator with period of about 10**26");
38 SetSeed(seed);
39}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Default destructor
43
47
48////////////////////////////////////////////////////////////////////////////////
49/// TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers
50/// Use a mask of 0xffffffffUL to make in work on 64 bit machines
51/// Periodicity of about 10**26
52/// Generate number in interval (0,1): 0 and 1 are not included in the interval
53
55{
56#define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) & 0xffffffffUL ) ^ ((((s <<a) & 0xffffffffUL )^s) >>b)
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 fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
62 fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
63 fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
64
65 UInt_t iy = fSeed ^ fSeed1 ^ fSeed2;
66 if (iy) return kScale*static_cast<Double_t>(iy);
67 return Rndm();
68}
69
70////////////////////////////////////////////////////////////////////////////////
71/// Return an array of n random numbers uniformly distributed in ]0, 1[
72
74{
75 const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
76
77 UInt_t iy;
78
79 for(Int_t i=0; i<n; i++) {
80 fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
81 fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
82 fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
83
84 iy = fSeed ^ fSeed1 ^ fSeed2;
85 if (iy) array[i] = (Float_t)(kScale*static_cast<Double_t>(iy));
86 else array[i] = Rndm();
87 }
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Return an array of n random numbers uniformly distributed in ]0, 1[
92
94{
95 const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
96
97 UInt_t iy;
98 for(Int_t i=0; i<n; i++) {
99 fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
100 fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
101 fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
102
103 iy = fSeed ^ fSeed1 ^ fSeed2;
104 if (iy) array[i] = kScale*static_cast<Double_t>(iy);
105 else array[i] = Rndm();
106 }
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Set the generator seed.
111/// If the seed given is zero, generate automatically seed values which
112/// are different every time by using TUUID.
113/// If a seed is given generate the other two needed for the generator state using
114/// a linear congruential generator
115/// The only condition, stated at the end of the 1999 L'Ecuyer paper is that the seeds
116/// must be greater than 1,7 and 15.
117/// Note that after setting the seed the generator is warmed up by calling it internally few
118/// times therefore the returned seed in TRandom2::GetSeed will be a different value.
119/// For this generator the user will have to store the provided seed externally
120/// if he wants to reproduce the random sequence
122{
123#define LCG(n) ((69069 * n) & 0xffffffffUL) // linear congurential generator
124
125 if (seed > 0) {
126 fSeed = LCG (seed);
127 if (fSeed < 2) fSeed += 2UL;
128 fSeed1 = LCG (fSeed);
129 if (fSeed1 < 8) fSeed1 += 8UL;
130 fSeed2 = LCG (fSeed1);
131 if (fSeed2 < 16) fSeed2 += 16UL;
132 } else {
133 // initialize using a TUUID
134 TUUID u;
135 UChar_t uuid[16];
136 u.GetUUID(uuid);
137 fSeed = UInt_t(uuid[3])*16777216 + UInt_t(uuid[2])*65536 + UInt_t(uuid[1])*256 + UInt_t(uuid[0]);
138 fSeed1 = UInt_t(uuid[7])*16777216 + UInt_t(uuid[6])*65536 + UInt_t(uuid[5])*256 + UInt_t(uuid[4]);
139 fSeed2 = UInt_t(uuid[11])*16777216 + UInt_t(uuid[10])*65536 + UInt_t(uuid[9])*256 + UInt_t(uuid[8]);
140 // use also the other bytes
141 UInt_t seed3 = UInt_t(uuid[15])*16777216 + UInt_t(uuid[14])*65536 + UInt_t(uuid[13])*256 + UInt_t(uuid[12]);
142 fSeed2 += seed3;
143
144 if (fSeed < 2) fSeed += 2UL;
145 if (fSeed1 < 8) fSeed1 += 8UL;
146 if (fSeed2 < 16) fSeed2 += 16UL;
147 }
148
149 // "warm it up" by calling it 6 times
150 for (int i = 0; i < 6; ++i)
151 Rndm();
152
153 return;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// \brief Returns one of the seeds of the generator.
158///
159/// \warning This is not the initial seed!
160///
161/// The internal state of the generator is described by three `UInt_t` numbers,
162/// called seed numbers, but they are not initial seeds. This function exposes
163/// one of them and can't provide full description of the generator state.
164///
166{
167 return fSeed;
168}
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
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)
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:44
Double_t Rndm() override
TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers Use a mask of 0xffffffffUL to make ...
Definition TRandom2.cxx:54
TRandom2(UInt_t seed=1)
Default constructor.
Definition TRandom2.cxx:34
void RndmArray(Int_t n, Float_t *array) override
Return an array of n random numbers uniformly distributed in ]0, 1[.
Definition TRandom2.cxx:73
void SetSeed(ULong_t seed=0) override
Set the generator seed.
Definition TRandom2.cxx:121
UInt_t GetSeed() const override
Returns one of the seeds of the generator.
Definition TRandom2.cxx:165
UInt_t fSeed1
Definition TRandom2.h:30
UInt_t fSeed2
Definition TRandom2.h:31
UInt_t fSeed
Definition TRandom.h:30
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