Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRandom1.cxx
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: Rene Brun from CLHEP & CERNLIB 04/05/2006
3
4/**
5
6\class TRandom1
7
8The Ranlux Random number generator class
9
10The algorithm for this random engine has been taken from the original
11implementation in FORTRAN by Fred James as part of CLHEP.
12
13The initialisation is carried out using a Multiplicative Congruential
14generator using formula constants of L'Ecuyer as described in "F.James,
15Comp. Phys. Comm. 60 (1990) 329-344".
16
17@ingroup Random
18
19*/
20
21
22#include "TRandom1.h"
23#include "TRandom3.h"
24#include "TMath.h"
25#include <cstdlib>
26
27// Number of instances with automatic seed selection
29
30// Maximum index into the seed table
31int TRandom1::fgMaxIndex = 215;
32const UInt_t fgSeedTable[215][2] = {
33 { 9876, 54321 },
34 { 1299961164, 253987020 },
35 { 669708517, 2079157264 },
36 { 190904760, 417696270 },
37 { 1289741558, 1376336092 },
38 { 1803730167, 324952955 },
39 { 489854550, 582847132 },
40 { 1348037628, 1661577989 },
41 { 350557787, 1155446919 },
42 { 591502945, 634133404 },
43 { 1901084678, 862916278 },
44 { 1988640932, 1785523494 },
45 { 1873836227, 508007031 },
46 { 1146416592, 967585720 },
47 { 1837193353, 1522927634 },
48 { 38219936, 921609208 },
49 { 349152748, 112892610 },
50 { 744459040, 1735807920 },
51 { 1983990104, 728277902 },
52 { 309164507, 2126677523 },
53 { 362993787, 1897782044 },
54 { 556776976, 462072869 },
55 { 1584900822, 2019394912 },
56 { 1249892722, 791083656 },
57 { 1686600998, 1983731097 },
58 { 1127381380, 198976625 },
59 { 1999420861, 1810452455 },
60 { 1972906041, 664182577 },
61 { 84636481, 1291886301 },
62 { 1186362995, 954388413 },
63 { 2141621785, 61738584 },
64 { 1969581251, 1557880415 },
65 { 1150606439, 136325185 },
66 { 95187861, 1592224108 },
67 { 940517655, 1629971798 },
68 { 215350428, 922659102 },
69 { 786161212, 1121345074 },
70 { 1450830056, 1922787776 },
71 { 1696578057, 2025150487 },
72 { 1803414346, 1851324780 },
73 { 1017898585, 1452594263 },
74 { 1184497978, 82122239 },
75 { 633338765, 1829684974 },
76 { 430889421, 230039326 },
77 { 492544653, 76320266 },
78 { 389386975, 1314148944 },
79 { 1720322786, 709120323 },
80 { 1868768216, 1992898523 },
81 { 443210610, 811117710 },
82 { 1191938868, 1548484733 },
83 { 616890172, 159787986 },
84 { 935835339, 1231440405 },
85 { 1058009367, 1527613300 },
86 { 1463148129, 1970575097 },
87 { 1795336935, 434768675 },
88 { 274019517, 605098487 },
89 { 483689317, 217146977 },
90 { 2070804364, 340596558 },
91 { 930226308, 1602100969 },
92 { 989324440, 801809442 },
93 { 410606853, 1893139948 },
94 { 1583588576, 1219225407 },
95 { 2102034391, 1394921405 },
96 { 2005037790, 2031006861 },
97 { 1244218766, 923231061 },
98 { 49312790, 775496649 },
99 { 721012176, 321339902 },
100 { 1719909107, 1865748178 },
101 { 1156177430, 1257110891 },
102 { 307561322, 1918244397 },
103 { 906041433, 360476981 },
104 { 1591375755, 268492659 },
105 { 461522398, 227343256 },
106 { 2145930725, 2020665454 },
107 { 1938419274, 1331283701 },
108 { 174405412, 524140103 },
109 { 494343653, 18063908 },
110 { 1025534808, 181709577 },
111 { 2048959776, 1913665637 },
112 { 950636517, 794796256 },
113 { 1828843197, 1335757744 },
114 { 211109723, 983900607 },
115 { 825474095, 1046009991 },
116 { 374915657, 381856628 },
117 { 1241296328, 698149463 },
118 { 1260624655, 1024538273 },
119 { 900676210, 1628865823 },
120 { 697951025, 500570753 },
121 { 1007920268, 1708398558 },
122 { 264596520, 624727803 },
123 { 1977924811, 674673241 },
124 { 1440257718, 271184151 },
125 { 1928778847, 993535203 },
126 { 1307807366, 1801502463 },
127 { 1498732610, 300876954 },
128 { 1617712402, 1574250679 },
129 { 1261800762, 1556667280 },
130 { 949929273, 560721070 },
131 { 1766170474, 1953522912 },
132 { 1849939248, 19435166 },
133 { 887262858, 1219627824 },
134 { 483086133, 603728993 },
135 { 1330541052, 1582596025 },
136 { 1850591475, 723593133 },
137 { 1431775678, 1558439000 },
138 { 922493739, 1356554404 },
139 { 1058517206, 948567762 },
140 { 709067283, 1350890215 },
141 { 1044787723, 2144304941 },
142 { 999707003, 513837520 },
143 { 2140038663, 1850568788 },
144 { 1803100150, 127574047 },
145 { 867445693, 1149173981 },
146 { 408583729, 914837991 },
147 { 1166715497, 602315845 },
148 { 430738528, 1743308384 },
149 { 1388022681, 1760110496 },
150 { 1664028066, 654300326 },
151 { 1767741172, 1338181197 },
152 { 1625723550, 1742482745 },
153 { 464486085, 1507852127 },
154 { 754082421, 1187454014 },
155 { 1315342834, 425995190 },
156 { 960416608, 2004255418 },
157 { 1262630671, 671761697 },
158 { 59809238, 103525918 },
159 { 1205644919, 2107823293 },
160 { 1615183160, 1152411412 },
161 { 1024474681, 2118672937 },
162 { 1703877649, 1235091369 },
163 { 1821417852, 1098463802 },
164 { 1738806466, 1529062843 },
165 { 620780646, 1654833544 },
166 { 1070174101, 795158254 },
167 { 658537995, 1693620426 },
168 { 2055317555, 508053916 },
169 { 1647371686, 1282395762 },
170 { 29067379, 409683067 },
171 { 1763495989, 1917939635 },
172 { 1602690753, 810926582 },
173 { 885787576, 513818500 },
174 { 1853512561, 1195205756 },
175 { 1798585498, 1970460256 },
176 { 1819261032, 1306536501 },
177 { 1133245275, 37901 },
178 { 689459799, 1334389069 },
179 { 1730609912, 1854586207 },
180 { 1556832175, 1228729041 },
181 { 251375753, 683687209 },
182 { 2083946182, 1763106152 },
183 { 2142981854, 1365385561 },
184 { 763711891, 1735754548 },
185 { 1581256466, 173689858 },
186 { 2121337132, 1247108250 },
187 { 1004003636, 891894307 },
188 { 569816524, 358675254 },
189 { 626626425, 116062841 },
190 { 632086003, 861268491 },
191 { 1008211580, 779404957 },
192 { 1134217766, 1766838261 },
193 { 1423829292, 1706666192 },
194 { 942037869, 1549358884 },
195 { 1959429535, 480779114 },
196 { 778311037, 1940360875 },
197 { 1531372185, 2009078158 },
198 { 241935492, 1050047003 },
199 { 272453504, 1870883868 },
200 { 390441332, 1057903098 },
201 { 1230238834, 1548117688 },
202 { 1242956379, 1217296445 },
203 { 515648357, 1675011378 },
204 { 364477932, 355212934 },
205 { 2096008713, 1570161804 },
206 { 1409752526, 214033983 },
207 { 1288158292, 1760636178 },
208 { 407562666, 1265144848 },
209 { 1071056491, 1582316946 },
210 { 1014143949, 911406955 },
211 { 203080461, 809380052 },
212 { 125647866, 1705464126 },
213 { 2015685843, 599230667 },
214 { 1425476020, 668203729 },
215 { 1673735652, 567931803 },
216 { 1714199325, 181737617 },
217 { 1389137652, 678147926 },
218 { 288547803, 435433694 },
219 { 200159281, 654399753 },
220 { 1580828223, 1298308945 },
221 { 1832286107, 169991953 },
222 { 182557704, 1046541065 },
223 { 1688025575, 1248944426 },
224 { 1508287706, 1220577001 },
225 { 36721212, 1377275347 },
226 { 1968679856, 1675229747 },
227 { 279109231, 1835333261 },
228 { 1358617667, 1416978076 },
229 { 740626186, 2103913602 },
230 { 1882655908, 251341858 },
231 { 648016670, 1459615287 },
232 { 780255321, 154906988 },
233 { 857296483, 203375965 },
234 { 1631676846, 681204578 },
235 { 1906971307, 1623728832 },
236 { 1541899600, 1168449797 },
237 { 1267051693, 1020078717 },
238 { 1998673940, 1298394942 },
239 { 1914117058, 1381290704 },
240 { 426068513, 1381618498 },
241 { 139365577, 1598767734 },
242 { 2129910384, 952266588 },
243 { 661788054, 19661356 },
244 { 1104640222, 240506063 },
245 { 356133630, 1676634527 },
246 { 242242374, 1863206182 },
247 { 957935844, 1490681416 }};
248
250
251////////////////////////////////////////////////////////////////////////////////
252/// \brief Luxury level is set in the same way as the original FORTRAN routine.
253///
254/// - level 0 (p=24): equivalent to the original RCARRY of Marsaglia
255/// and Zaman, very long period, but fails many tests.
256/// - level 1 (p=48): considerable improvement in quality over level 0,
257/// now passes the gap test, but still fails spectral test.
258/// - level 2 (p=97): passes all known tests, but theoretically still
259/// defective.
260/// - level 3 (p=223): DEFAULT VALUE. Any theoretically possible
261/// correlations have very small chance of being observed.
262/// - level 4 (p=389): highest possible luxury, all 24 bits chaotic.
264 : fIntModulus(0x1000000),
265 fMantissaBit24( TMath::Power(0.5,24.) ),
266 fMantissaBit12( TMath::Power(0.5,12.) )
267{
268 UInt_t seedlist[2]={0,0};
269
270 fTheSeeds = &fSeed;
271 fLuxury = lux;
272 SetSeed2(seed, fLuxury);
273 // in case seed = 0 SetSeed2 calls already SetSeeds
274 if (seed != 0) {
275 // setSeeds() wants a zero terminated array!
276 seedlist[0]=fSeed;
277 seedlist[1]=0;
278 SetSeeds(seedlist, fLuxury);
279 }
280}
281
282////////////////////////////////////////////////////////////////////////////////
283/// \brief Default constructor.
285 : fIntModulus(0x1000000),
286 fMantissaBit24( TMath::Power(0.5,24.) ),
287 fMantissaBit12( TMath::Power(0.5,12.) )
288{
289 fTheSeeds = &fSeed;
290 UInt_t seed;
291 UInt_t seedlist[2]={0,0};
292
293 fLuxury = 3;
294 int cycle = abs(int(fgNumEngines/fgMaxIndex));
295 int curIndex = abs(int(fgNumEngines%fgMaxIndex));
296 fgNumEngines +=1;
297 UInt_t mask = ((cycle & 0x007fffff) << 8);
298 GetTableSeeds( seedlist, curIndex );
299 seed = seedlist[0]^mask;
300 SetSeed2(seed, fLuxury);
301
302 // setSeeds() wants a zero terminated array!
303 seedlist[0]=fSeed; //<=============
304 seedlist[1]=0;
305 SetSeeds(seedlist, fLuxury);
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// \brief Constructor.
310TRandom1::TRandom1(Int_t rowIndex, Int_t colIndex, Int_t lux)
311 : fIntModulus(0x1000000),
312 fMantissaBit24( TMath::Power(0.5,24.) ),
313 fMantissaBit12( TMath::Power(0.5,12.) )
314{
315 fTheSeeds = &fSeed;
316 UInt_t seed;
317 UInt_t seedlist[2]={0,0};
318
319 fLuxury = lux;
320 int cycle = abs(int(rowIndex/fgMaxIndex));
321 int row = abs(int(rowIndex%fgMaxIndex));
322 int col = abs(int(colIndex%2));
323 UInt_t mask = (( cycle & 0x000007ff ) << 20 );
324 GetTableSeeds( seedlist, row );
325 seed = ( seedlist[col] )^mask;
326 SetSeed2(seed, fLuxury);
327
328 // setSeeds() wants a zero terminated array!
329 seedlist[0]=fSeed;
330 seedlist[1]=0;
331 SetSeeds(seedlist, fLuxury);
332}
333
334////////////////////////////////////////////////////////////////////////////////
335/// \brief Destructor.
337{
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// \brief Static function returning the seeds table.
343{
344 if ((index >= 0) && (index < 215)) {
345 seeds[0] = fgSeedTable[index][0];
346 seeds[1] = fgSeedTable[index][1];
347 }
348 else seeds = nullptr;
349}
350
351////////////////////////////////////////////////////////////////////////////////
352/// \brief Returns a random number in ]0,1]
354{
355 float next_random;
356 float uni;
357 int i;
358
360 if(uni < 0. ) {
361 uni += 1.0;
363 } else {
364 fCarry = 0.;
365 }
366
367 fFloatSeedTable[fIlag] = uni;
368 fIlag --;
369 fJlag --;
370 if(fIlag < 0) fIlag = 23;
371 if(fJlag < 0) fJlag = 23;
372
373 if( uni < fMantissaBit12 ){
375 if( uni == 0) uni = fMantissaBit24 * fMantissaBit24;
376 }
377 next_random = uni;
378 fCount24 ++;
379
380// every 24th number generation, several random numbers are generated
381// and wasted depending upon the fLuxury level.
382
383 if(fCount24 == 24 ) {
384 fCount24 = 0;
385 for( i = 0; i != fNskip ; i++) {
387 if(uni < 0. ) {
388 uni += 1.0;
390 } else {
391 fCarry = 0.;
392 }
393 fFloatSeedTable[fIlag] = uni;
394 fIlag --;
395 fJlag --;
396 if(fIlag < 0)fIlag = 23;
397 if(fJlag < 0) fJlag = 23;
398 }
399 }
400 return static_cast<Double_t>(next_random);
401}
402
403////////////////////////////////////////////////////////////////////////////////
404/// \brief Returns an array of random numbers in ]0,1]
406{
407 for (Int_t i=0;i<size;i++) vect[i] = Rndm();
408}
409
410////////////////////////////////////////////////////////////////////////////////
411/// \brief Returns an array of random numbers in ]0,1[
413{
414 float next_random;
415 float uni;
416 int i;
417 int index;
418
419 for (index=0; index<size; ++index) {
421 if(uni < 0. ) {
422 uni += 1.0;
424 } else {
425 fCarry = 0.;
426 }
427
428 fFloatSeedTable[fIlag] = uni;
429 fIlag --;
430 fJlag --;
431 if(fIlag < 0) fIlag = 23;
432 if(fJlag < 0) fJlag = 23;
433
434 if( uni < fMantissaBit12 ){
436 if( uni == 0) uni = fMantissaBit24 * fMantissaBit24;
437 }
438 next_random = uni;
439 vect[index] = (double)next_random;
440 fCount24 ++;
441
442// every 24th number generation, several random numbers are generated
443// and wasted depending upon the fLuxury level.
444
445 if(fCount24 == 24 ) {
446 fCount24 = 0;
447 for( i = 0; i != fNskip ; i++) {
449 if(uni < 0. ) {
450 uni += 1.0;
452 } else {
453 fCarry = 0.;
454 }
455 fFloatSeedTable[fIlag] = uni;
456 fIlag --;
457 fJlag --;
458 if(fIlag < 0)fIlag = 23;
459 if(fJlag < 0) fJlag = 23;
460 }
461 }
462 }
463}
464
465////////////////////////////////////////////////////////////////////////////////
466/// \brief Set the state of the generator providing an array of seeds.
467///
468/// The array of seeds can be of size 24 or less. In case of an array of n seeds with n < 24
469/// the n+1 element must be equal to zero.
470/// The other elements are the initialized using a Multiplicative
471/// Congruential generator using formula constants of L'Ecuyer
472/// as described in "A review of pseudorandom number generators"
473/// (Fred James) published in Computer Physics Communications 60 (1990)
474/// pages 329-344
475///
476/// \param[in] seeds array of seeds (size from 1 to 24)
477/// \param[in] lux Luxury level
478///
479void TRandom1::SetSeeds(const UInt_t *seeds, int lux)
480{
481 const int ecuyer_a = 53668;
482 const int ecuyer_b = 40014;
483 const int ecuyer_c = 12211;
484 const int ecuyer_d = 2147483563;
485
486 const int lux_levels[5] = {0,24,73,199,365};
487 int i;
488 UInt_t int_seed_table[24];
489 Long64_t k_multiple,next_seed;
490 const UInt_t *seedptr;
491
492 fTheSeeds = seeds;
493 seedptr = seeds;
494
495 if(seeds == nullptr) {
496 SetSeed2(fSeed,lux);
497 fTheSeeds = &fSeed;
498 return;
499 }
500
501 fSeed = *seeds;
502
503// number of additional random numbers that need to be 'thrown away'
504// every 24 numbers is set using fLuxury level variable.
505
506 if( (lux > 4)||(lux < 0) ) {
507 if(lux >= 24) {
508 fNskip = lux - 24;
509 } else {
510 fNskip = lux_levels[3]; // corresponds to default fLuxury level
511 }
512 } else {
513 fLuxury = lux;
514 fNskip = lux_levels[fLuxury];
515 }
516
517 for( i = 0;(i != 24)&&(*seedptr != 0);i++) {
518 int_seed_table[i] = *seedptr % fIntModulus;
519 seedptr++;
520 }
521
522 if(i != 24){
523 next_seed = int_seed_table[i-1];
524 for(;i != 24;i++) {
525 k_multiple = next_seed / ecuyer_a;
526 next_seed = ecuyer_b * (next_seed - k_multiple * ecuyer_a)
527 - k_multiple * ecuyer_c ;
528 if(next_seed < 0)next_seed += ecuyer_d;
529 int_seed_table[i] = next_seed % fIntModulus;
530 }
531 }
532
533 for(i = 0;i != 24;i++)
534 fFloatSeedTable[i] = int_seed_table[i] * fMantissaBit24;
535
536 fIlag = 23;
537 fJlag = 9;
538 fCarry = 0. ;
539
540 if( fFloatSeedTable[23] == 0. ) fCarry = fMantissaBit24;
541
542 fCount24 = 0;
543}
544
545////////////////////////////////////////////////////////////////////////////////
546/// \brief Set the state of the generator providing a single seed value and a
547/// luxury level.
548///
549/// The initialisation of the other state values is carried out using a Multiplicative
550/// Congruential generator using formula constants of L'Ecuyer
551/// as described in "A review of pseudorandom number generators"
552/// (Fred James) published in Computer Physics Communications 60 (1990)
553/// pages 329-344
554///
555/// \note When provided with seed = 0, a random and unique seed is generated.
556///
557/// \param[in] seed seed value (note special case if seed=0)
558/// \param[in] lux Luxury level
559///
560///
561void TRandom1::SetSeed2(UInt_t seed, int lux)
562{
563 // case of seed == 0
564 // use a random seed based on TRandom3(0) which is based on the UUID
565 if (seed == 0) {
566 UInt_t randSeeds[25];
567 TRandom3 r2(0);
568 for (int j = 0; j < 24; ++j)
569 randSeeds[j] = static_cast<UInt_t>(4294967296. * r2.Rndm());
570 randSeeds[24] = 0;
571 SetSeeds(randSeeds, lux);
572 return;
573 }
574
575 // seed List requires a zero terminated array in case the size of the array is less that the
576 // state size (24)
577 UInt_t seedList[2] = {seed, 0};
578 SetSeeds(seedList, lux);
579}
580
581////////////////////////////////////////////////////////////////////////////////
582/// \brief Set the state of the generator providing a single seed value and using
583/// the luxury level defined when constructing the class.
584///
585/// The initialisation of the other state values is carried out using a Multiplicative
586/// Congruential generator.
587///
588/// \note When seed = 0, a random and unique seed is generated.
589///
590/// \param[in] seed seed value (note special case if seed=0)
591///
592
594{
595 // Set RanLux seed using the luxury level provided in the constructor
596 SetSeed2(seed,fLuxury);
597}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long ULong_t
Definition RtypesCore.h:55
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
long long Long64_t
Definition RtypesCore.h:80
#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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
const UInt_t fgSeedTable[215][2]
Definition TRandom1.cxx:32
The Ranlux Random number generator class.
Definition TRandom1.h:27
const Double_t fMantissaBit12
Definition TRandom1.h:42
Float_t fFloatSeedTable[24]
Definition TRandom1.h:35
TRandom1()
Default constructor.
Definition TRandom1.cxx:284
Int_t fIlag
Definition TRandom1.h:32
void RndmArray(Int_t size, Float_t *vect) override
Returns an array of random numbers in ]0,1].
Definition TRandom1.cxx:405
Int_t fNskip
Definition TRandom1.h:30
Int_t fCount24
Definition TRandom1.h:34
~TRandom1() override
Destructor.
Definition TRandom1.cxx:336
Double_t Rndm() override
Returns a random number in ]0,1].
Definition TRandom1.cxx:353
const UInt_t * fTheSeeds
Definition TRandom1.h:40
Int_t fJlag
Definition TRandom1.h:33
static Int_t fgMaxIndex
Definition TRandom1.h:39
void SetSeed(ULong_t seed) override
Set the state of the generator providing a single seed value and using the luxury level defined when ...
Definition TRandom1.cxx:593
Float_t fCarry
Definition TRandom1.h:36
const Double_t fMantissaBit24
Temporary array of seed values (transient)
Definition TRandom1.h:41
const Int_t fIntModulus
Definition TRandom1.h:37
virtual void SetSeeds(const UInt_t *seeds, Int_t lux=3)
Set the state of the generator providing an array of seeds.
Definition TRandom1.cxx:479
virtual void SetSeed2(UInt_t seed, Int_t lux=3)
Set the state of the generator providing a single seed value and a luxury level.
Definition TRandom1.cxx:561
static void GetTableSeeds(UInt_t *seeds, Int_t index)
Returns an array of seed values stored in the table, given the index.
Definition TRandom1.cxx:342
static Int_t fgNumEngines
Definition TRandom1.h:38
Int_t fLuxury
Definition TRandom1.h:31
Random number generator class based on M.
Definition TRandom3.h:27
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom3.cxx:99
UInt_t fSeed
Definition TRandom.h:30
TMath.
Definition TMathBase.h:35