Skip to content

Commit 15903c3

Browse files
medeirosdevguitargeek
authored andcommitted
[math] Have TRandom2/3::Rndm() delegate to operator()()
Avoids duplicating the Tausworthe/Mersenne Twister state-advance logic. Rndm() now calls operator()() to get the raw integer and then scales it to a double, keeping a single source of truth for the generator core. The TAUSWORTHE macro is moved to file scope so RndmArray can still use it.
1 parent 6ede23e commit 15903c3

2 files changed

Lines changed: 6 additions & 43 deletions

File tree

math/mathcore/src/TRandom2.cxx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The publications are available online at
2626
#include "TRandom2.h"
2727
#include "TUUID.h"
2828

29+
#define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) & 0xffffffffUL ) ^ ((((s <<a) & 0xffffffffUL )^s) >>b)
30+
2931

3032

3133
////////////////////////////////////////////////////////////////////////////////
@@ -53,17 +55,11 @@ TRandom2::~TRandom2()
5355

5456
Double_t TRandom2::Rndm()
5557
{
56-
#define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) & 0xffffffffUL ) ^ ((((s <<a) & 0xffffffffUL )^s) >>b)
57-
5858
// scale by 1./(Max<UINT> + 1) = 1./4294967296
5959
const double kScale = 2.3283064365386963e-10; // range in 32 bit ( 1/(2**32)
6060

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);
61+
UInt_t iy = operator()();
62+
if (iy) return kScale * static_cast<Double_t>(iy);
6763
return Rndm();
6864
}
6965

math/mathcore/src/TRandom3.cxx

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,42 +106,9 @@ TRandom3::~TRandom3()
106106

107107
Double_t TRandom3::Rndm()
108108
{
109-
UInt_t y;
110-
111-
const Int_t kM = 397;
112-
const Int_t kN = 624;
113-
const UInt_t kTemperingMaskB = 0x9d2c5680;
114-
const UInt_t kTemperingMaskC = 0xefc60000;
115-
const UInt_t kUpperMask = 0x80000000;
116-
const UInt_t kLowerMask = 0x7fffffff;
117-
const UInt_t kMatrixA = 0x9908b0df;
118-
119-
if (fCount624 >= kN) {
120-
Int_t i;
121-
122-
for (i=0; i < kN-kM; i++) {
123-
y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
124-
fMt[i] = fMt[i+kM] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
125-
}
126-
127-
for ( ; i < kN-1 ; i++) {
128-
y = (fMt[i] & kUpperMask) | (fMt[i+1] & kLowerMask);
129-
fMt[i] = fMt[i+kM-kN] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
130-
}
131-
132-
y = (fMt[kN-1] & kUpperMask) | (fMt[0] & kLowerMask);
133-
fMt[kN-1] = fMt[kM-1] ^ (y >> 1) ^ ((y & 0x1) ? kMatrixA : 0x0);
134-
fCount624 = 0;
135-
}
136-
137-
y = fMt[fCount624++];
138-
y ^= (y >> 11);
139-
y ^= ((y << 7 ) & kTemperingMaskB );
140-
y ^= ((y << 15) & kTemperingMaskC );
141-
y ^= (y >> 18);
142-
143109
// 2.3283064365386963e-10 == 1./(UINT_MAX+1UL) -> then returned value cannot be = 1.0
144-
if (y) return ( (Double_t) y * 2.3283064365386963e-10); // * Power(2,-32)
110+
UInt_t y = operator()();
111+
if (y) return ((Double_t) y * 2.3283064365386963e-10); // * Power(2,-32)
145112
return Rndm();
146113
}
147114

0 commit comments

Comments
 (0)