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