Logo ROOT   6.08/07
Reference Guide
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 
8 Random number generator class based on the maximally quidistributed combined
9 Tausworthe generator by L'Ecuyer.
10 
11 The period of the generator is 2**88 (about 10**26) and it uses only 3 words
12 for the state.
13 
14 For more information see:
15 P. L'Ecuyer, Mathematics of Computation, 65, 213 (1996)
16 P. L'Ecuyer, Mathematics of Computation, 68, 225 (1999)
17 
18 The publication 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 "TRandom3.h"
28 #include "TUUID.h"
29 
30 
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 ///*-*-*-*-*-*-*-*-*-*-*default constructor*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
35 ///*-* ===================
36 
38 {
39  SetName("Random2");
40  SetTitle("Random number generator with period of about 10**26");
41  SetSeed(seed);
42 }
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 ///*-*-*-*-*-*-*-*-*-*-*default destructor*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
46 ///*-* ==================
47 
49 {
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// TausWorth generator from L'Ecuyer, uses as seed 3x32bits integers
54 /// Use a mask of 0xffffffffUL to make in work on 64 bit machines
55 /// Periodicity of about 10**26
56 /// Generate number in interval (0,1) : 0 and 1 are not included in the interval
57 
59 {
60 #define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) & 0xffffffffUL ) ^ ((((s <<a) & 0xffffffffUL )^s) >>b)
61 
62  // scale by 1./(Max<UINT> + 1) = 1./4294967296
63  const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
64 
65  fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
66  fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
67  fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
68 
69  UInt_t iy = fSeed ^ fSeed1 ^ fSeed2;
70  if (iy) return kScale*static_cast<Double_t>(iy);
71  return Rndm();
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Return an array of n random numbers uniformly distributed in ]0,1]
76 
78 {
79  const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
80 
81  UInt_t iy;
82 
83  for(Int_t i=0; i<n; i++) {
84  fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
85  fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
86  fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
87 
88  iy = fSeed ^ fSeed1 ^ fSeed2;
89  if (iy) array[i] = (Float_t)(kScale*static_cast<Double_t>(iy));
90  else array[i] = Rndm();
91  }
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Return an array of n random numbers uniformly distributed in ]0,1]
96 
98 {
99  const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
100 
101  UInt_t iy;
102  for(Int_t i=0; i<n; i++) {
103  fSeed = TAUSWORTHE (fSeed, 13, 19, 4294967294UL, 12);
104  fSeed1 = TAUSWORTHE (fSeed1, 2, 25, 4294967288UL, 4);
105  fSeed2 = TAUSWORTHE (fSeed2, 3, 11, 4294967280UL, 17);
106 
107  iy = fSeed ^ fSeed1 ^ fSeed2;
108  if (iy) array[i] = kScale*static_cast<Double_t>(iy);
109  else array[i] = Rndm();
110  }
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Set the generator seed.
115 /// If the seed given is zero, generate automatically seed values which
116 /// are different every time by using TRandom3 and TUUID
117 /// If a seed is given generate the other two needed for the generator state using
118 /// a linear congruential generator
119 /// The only condition, stated at the end of the 1999 L'Ecuyer paper is that the seeds
120 /// must be greater than 1,7 and 15.
121 
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 
146  // TRandom r3(0);
147  // fSeed = static_cast<UInt_t> (4294967296.*r3.Rndm());
148  // fSeed1 = static_cast<UInt_t> (4294967296.*r3.Rndm());
149  // fSeed2 = static_cast<UInt_t> (4294967296.*r3.Rndm());
150 
151  if (fSeed < 2) fSeed += 2UL;
152  if (fSeed1 < 8) fSeed1 += 8UL;
153  if (fSeed2 < 16) fSeed2 += 16UL;
154  }
155 
156  // "warm it up" by calling it 6 times
157  for (int i = 0; i < 6; ++i)
158  Rndm();
159 
160  return;
161 }
162 
float Float_t
Definition: RtypesCore.h:53
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:29
UInt_t fSeed2
Definition: TRandom2.h:33
virtual void SetSeed(ULong_t seed=0)
Set the generator seed.
Definition: TRandom2.cxx:122
UInt_t fSeed1
Definition: TRandom2.h:32
int Int_t
Definition: RtypesCore.h:41
virtual ~TRandom2()
-*-*-*-*-*-*-*-*-*-*default destructor-*-*-*-*-*-*-*-*-*-*-*-*-*-* *-* ================== ...
Definition: TRandom2.cxx:48
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition: TUUID.h:44
virtual void RndmArray(Int_t n, Float_t *array)
Return an array of n random numbers uniformly distributed in ]0,1].
Definition: TRandom2.cxx:77
unsigned int UInt_t
Definition: RtypesCore.h:42
const Float_t kScale
Definition: TGQuartz.mm:47
virtual Double_t Rndm()
TausWorth generator from L&#39;Ecuyer, uses as seed 3x32bits integers Use a mask of 0xffffffffUL to make ...
Definition: TRandom2.cxx:58
UInt_t fSeed
Definition: TRandom.h:34
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
#define LCG(n)
unsigned long ULong_t
Definition: RtypesCore.h:51
#define TAUSWORTHE(s, a, b, c, d)
unsigned char UChar_t
Definition: RtypesCore.h:34
void GetUUID(UChar_t uuid[16]) const
Return uuid in specified buffer (16 byte = 128 bits).
Definition: TUUID.cxx:661
const Int_t n
Definition: legend1.C:16
gr SetName("gr")