Random Number
Random Number
9
∗ J = 1 + int(10 × ri) better in compare with
∗ J = 1 + mod(100000 × ri, 10)
– Gradual improvement of a,c,m .. (Computer Phys. Comm., 60
(1990) 329)
∗ a = 23, m = 108 + 1
∗ a = 65539, m = 229
∗ a = 69069, m = 232
∗ a = 75 = 16907, m = 231 − 1
∗ a = 1664525, m = 232
∗ a = 742938285, m = 231 − 1
∗ a = 40014, m = 2147483563
∗ a = 40692, m = 2147483399
∗ a = 515, m = 247
– Extension, ri+1 = mod(a × ri + b × ri−1 + c, m)
• Fibonacci series : ri = mod(ri−p ⊙ ri−q , m)
where ⊙ is some binary or logical operation (ordinary addition or sub-
traction, but not exclusive-or) and p and q are the lags with p > q
10
Though repetition number is very large, random numbers stay mainly in the
plane (A large sequential correlation) : George Marsagla
0.9 0.9
0.8 0.8
0.7 0.7
0.6 0.6
0.5 0.5
0.4 0.4
0.3 0.3
0.2 0.2
0.1 0.1
0.9 0.9
0.8 0.8
0.7 0.7
0.6 0.6
0.5 0.8 0.9 0.5 0.8 0.9
0.4 0.4
0.3 0.5 0.6 0.7 0.3 0.5 0.6 0.7
0.2
0.1 0.3 0.4 0.2
0.1 0.3 0.4
0.1 0.2 0.1 0.2
GetUint() {
static unsigned int m_z = 1, m_w = 2;
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w; }
Subtractive method
11
Random number generation for any arbitrary function
RNow,
∞
generate a random number with a given probability, g(y) with
−∞ g(y)dy = 1
Simply, let a function f (x) ranging from 0 toR1 maps to another function
1 R2
f (y) ranging from 0 to 2, Cumulative function 0 f (x)dx = 0 g(y)dy (other
example, your x-co-ordinate is changing from cm to m (100 times in x, but
1/100 times in f (x).
12
Generation of some simple distribution
R
• Isotropic azimuthal distribution : f (φ) = k = (1/2π), x = (1/2π)dφ =
(1/2π)φ + c
At φ = 0, x = 0 ⇒ c = 0 or at φ = 2π, x = 1 ⇒ c = 0, thus φ = 2πx
• Isotropic
R distribution : Surface area is sin θdθdφ. So, f (θ) = (1/2) sin θ.
∴ x = (1/2) sin θdθ = −(1/2) cos θ + c At x = 0, θ = 0 ⇒ c = 1/2 or
x = 1, θ = π ⇒ c = 1/2
∴ x = (1/2)(1 − cos θ) ⇒ θ = cos−1 (1 − 2x)
• Exponential
R decay or absorption : f (τ ) = (1/τ )exp(−t/τ ), x =
(1/τ ) exp(−t/τ ) + c = exp(−t/τ ) + c
At x = 0, t = 0 ⇒ c = 1
∴ x = exp(−t/τ ) + 1 ⇒ t = −τ loge(1 − x) ∼ t = −τ loge (x)
• Cosmic muon flux, dN/dE ∝ E −α within a range, Elow to Ehigh, where
α>1
−1
1 −(α−1) −(α−1)
N ormalisation(N ) = Elow − Ehigh
α−1
N
dx = N E −α dE → x = − α−1 E −(α−1) + c
N −(α−1)
at x = 0, E = Elow =⇒ c = α−1 Elow
h i
N −(α−1) −(α−1)
Thus, x = α−1 Elow −E
−1/(α−1)
−(α−1) (α − 1)
E = Elow − x
N
13
• Probability distribution Ris continuous but not conventionally integrated
x
analytically, F (cos θ) = −1 f (cos θ)d(cos θ) = x
-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1
1 1
0.8 0.8
F(cosΘ)
0.6 0.6
f(cosΘ
0.4 0.4
0.2 0.2
0 0
-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1
CosΘ
14
Normal/Gaussian Distribution : G.E.P. Box and M.E. Muller, 1958
Now consider the Gaussian distribution with mean zero and σ=1, φ(y; 0, 1) =
√1 exp(−y 2 /2) in the range −∞ < y < ∞
2π
15
N N N
! ! !
√
r
−1
X xi 1 X xi 1 12 X N
y = σm − = − 12N = xi −
i=1
N 2 i=1
N 2 N i=1
2
χ2 , ndf, µ, σ, RMS
U1 U2 U3 U4
3 3
10 10
3
10
3982.1 3760.3 2948.2 1095.5
102 102
33 47 57 63
3 -0.014 102 -0.002 10
0.009 10
-0.013
10
1.771 0.97 0.939 0.976
1 0.999 1 1 1 1.001
-5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5
U5 U6 U7 U8
3 3 3 3
10 10 10 10
622.2 444.1 333 339
102 102 102 102
66 71 72 73
10
-0.003 10
-0.002 10
-0.004 10
-0.008
0.987 0.99 0.995 0.992
1 1.001 1 1.002 1 1.003 1 1.002
-5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5
U9 3
U10 U11 U12
3
10 10 3 3
10 10
170.7 165.1 144.2 123.5
102 102
102 102
71 74 72 76
10 -0.007 10 -0.004 10
-0.005 10
-0.004
0.997 0.998 0.999 0.999
1 1
0.999 1.001 1 1.001 1 1.001
-5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5 -5 -4 -3 -2 -1 0 1 2 3 4 5
U13 U14 3
U15 U16
3 3 3
10 10 10 10
16
Rejection and acceptance
x→
1/(2π)
0 x→ 1
17
• Generate distribution
1 1
f (x) = ,
π 1 + x2
with range 0 ≤ x ≤ 1
• Transformation method :
Z x
1 1 1
y= 2
dx = tan−1 (x) ⇒ x = tan (π y)
0 π1+x π
• Rejection and acceptance method : Pmx = 1/π, verify these two methods
18
Special function - discrete numbers
n! r
Binomial distribution : p(r; n, p) = (n−r)!r! p (1 − p)n−r
19