SlideShare a Scribd company logo
RANDOM NUMBER GENERATION
• Measured data often have random fluctuations .
• This may be due to inaccurate measurements .
• It may also be due to other external influences .
• Often we know or believe there is a deterministic model .
• (i.e., the process can be modeled by a deterministic equation .)
• However, deterministic equations can also have random behavior !
• The study of such equations is sometimes called chaos theory .
• We will look at a simple example, namely, the logistic equation .
The Logistic Equation
A simple deterministic model of population growth is
xk+1 = λ xk , k = 1, 2, · · · ,
for given λ , (λ ≥ 0) , and for given x0 , (x0 ≥ 0) .
The solution is
xk = λk
x0 , k = 1, 2, · · · .
Thus
If 0 ≤ λ < 1 then xk → 0 as k → ∞ (”extinction”) .
If λ > 1 then xk → ∞ as k → ∞ (”exponential growth”) .
A somewhat more realistic population growth model is
xk+1 = λ xk (1 − xk) , k = 1, 2, · · · ,
• This model is known as the logistic equation .
• The maximum sustainable population is 1 .
• λ is given, (0 ≤ λ ≤ 4) .
• x0 is given, (0 ≤ x0 ≤ 1) .
• Then 0 ≤ xk ≤ 1 for all k . (Prove this !)
QUESTION : How does the sequence {xk}∞
k=1 depend on λ ?
EXERCISE :
xk+1 = λ xk (1 − xk) , k = 1, 2, · · · ,
• Divide the interval [0, 1] into 200 subintervals .
• Compute xk for k = 1, 2, · · · 50 .
• Count the xk’s in each subinterval.
• Determine the percentage of xk’s in each subinterval.
• Present the result in a diagram .
• Do this for different choices of λ (0 ≤ λ ≤ 4) .
0.0 0.2 0.4 0.6 0.8
x
0.0
0.1
0.2
0.3
0.4
0.5
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 0.9 , x0 = 0.77 , 50 iterations , 200 subintervals .
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 1.7 , x0 = 0.77 , 50 iterations , 200 subintervals .
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.00
0.01
0.02
0.03
0.04
0.05
0.06
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.77 , x0 = 0.77 , 50 iterations , 200 subintervals .
EXERCISE :
xk+1 = λ xk (1 − xk) , k = 1, 2, · · · ,
Do the same as in the preceding example, but now
• Compute xk for k = 1, 2, · · · 1, 000, 000 !
• Do not record the first 200 iterations.
• ( This to eliminate transient effects .)
• You will see that there is a fixed point (a cycle of period 1 ) .
0.0 0.2 0.4 0.6 0.8 1.0
x
0.0
0.2
0.4
0.6
0.8
1.0
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 1.7 , 1, 000, 000 iterations , 200 subintervals .
There is a fixed point (a cycle of period 1 ) .
0.0 0.2 0.4 0.6 0.8 1.0
x
0.00
0.05
0.10
0.15
0.20
0.25
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.46 , 1, 000, 000 iterations , 200 subintervals .
There is a cycle of period 4 .
0.0 0.2 0.4 0.6 0.8 1.0
x
0.00
0.02
0.04
0.06
0.08
0.10
0.12
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.561 , 1, 000, 000 iterations , 200 subintervals .
There is a cycle of period 8 .
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.00
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.6 , 1, 000, 000 iterations , 200 subintervals .
(The Figure on the right only shows 100 of these iterations.)
There is apparent chaotic behavior .
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.000
0.005
0.010
0.015
0.020
0.025
0.030
0.035
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.77 , 1, 000, 000 iterations , 200 subintervals .
(The Figure on the right only shows 100 of these iterations.)
There is apparent chaotic behavior .
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.000
0.005
0.010
0.015
0.020
0.025
0.030
0.035
0.040
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.89 , 1, 000, 000 iterations , 200 subintervals .
(The Figure on the right only shows 100 of these iterations.)
There is apparent chaotic behavior .
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.000
0.005
0.010
0.015
0.020
0.025
0.030
0.035
p(x)
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
y
Percentage per interval Graphical interpretation.
λ = 3.99 , 1, 000, 000 iterations , 200 subintervals .
(The Figure on the right only shows 100 of these iterations.)
There is apparent chaotic behavior .
CONCLUSIONS :
• The behavior of the logistic equation depends on λ .
• For certain values of λ we see fixed points .
• For other values of λ there are cycles .
• For yet other values of λ there is seemingly random behavior .
• Many other deterministic equations have ”complex behavior ” !
• Nature is complex !
Generating Random Numbers
The logistic equation is a recurrence relation of the form
xk+1 = f(xk) , k = 1, 2, 3, · · · .
Most random number generators also have this form.
For the logistic equation we did not see sequences {xk}N
k=1 having
• a uniform distribution,
• a normal distribution,
• another known distribution.
QUESTION :
• How to generate uniform (and other) random numbers ?
• ( These are useful in computer simulations .)
Generating Uniformly Distributed Random Numbers
Uniformly distributed random numbers can also be generated by
a recurrence relation of the form
xk+1 = f(xk) , k = 1, 2, 3, · · · .
Unlike the logistic equation, the xk are most often integers .
The recurrence relation typically has the form
xk+1 = (n xk) mod p .
where p is a large prime number , and n a large integer , with
p 6 | n .
The following fact is useful :
THEOREM :
Let p be a prime number, and n an integer such that
p 6 | n .
Then the function
f : { 0, 1, 2, · · · , p − 1 } → { 0, 1, 2, · · ·, p − 1 } ,
given by
f(x) = (n x) mod p ,
is one-to-one (and hence onto, a bijection, and invertible).
p prime , p 6 | n ⇒ (n x) mod p is 1 − 1
EXAMPLE :
p = 7 and n = 12 .
x 12x 12x mod 7
0 0 0
1 12 5
2 24 3
3 36 1
4 48 6
5 60 4
6 72 2
Invertible !
NOTE : The values of 12x mod 7 look somewhat random !
p prime , p 6 | n ⇒ (n x) mod p is 1 − 1
EXAMPLE :
p = 6 and n = 2 .
x 2x 2x mod 6
0 0 0
1 2 2
2 4 4
3 6 0
4 8 2
5 10 4
Not Invertible .
p prime , p 6 | n ⇒ (n x) mod p is 1 − 1
EXAMPLE :
p = 6 and n = 13 .
x 13x 13x mod 6
0 0 0
1 13 1
2 26 2
3 39 3
4 52 4
5 65 5
Invertible . ( So ? )
NOTE : The numbers in the right hand column don’t look random !
p prime , p 6 | n ⇒ (n x) mod p is 1 − 1
PROOF : By contradiction : Suppose the function is not 1 − 1 .
Then there are distinct integers x1 , x2 ∈ { 0, 1, 2, · · ·, p − 1 } ,
such that
(nx1) mod p = k and (nx2) mod p = k ,
where also k ∈ { 0, 1, 2, · · ·, p − 1 } . It follows that
p | n(x1 − x2) , ( Why ? )
Since p is prime and p 6 | n it follows that
p | (x1 − x2) .
Thus x1 −x2 = 0 ( Why ? ) , i.e. , x1 = x2 . Contradiction !
For given x0 , an iteration of the form
xk = (n xk−1) mod p , k = 1, 2, · · · , p − 1 .
can be used to generate random numbers .
• Here p is a large prime number .
• The value of n is also large .
• The integer n must not be divisible by p .
• Do not start with x0 = 0 (because it is a fixed point !) .
• Be aware of cycles (of period less than p − 1) !
REMARK : Actually, more often used is an iteration of the form
xk = (n xk−1 + m) mod p , k = 1, 2, · · · , p − 1 .
EXAMPLE : As a simple example, take again p = 7 and n = 12 :
x 12x 12x mod 7
0 0 0
1 12 5
2 24 3
3 36 1
4 48 6
5 60 4
6 72 2
With x1 = 1 the recurrence relation
xk+1 = f(xk) , where f(x) = 12x mod 7 ,
generates the sequence
1 → 5 → 4 → 6 → 2 → 3 → 1 → 5 → · · ·
which is a cycle of maximal period p − 1 (here 6) .
f(x) sequence cycle period
5x mod 7 1 → 5 → 4 → 6 → 2 → 3 → 1 6
6x mod 7 1 → 6 → 1 → 6 → 1 → 6 → 1 2
8x mod 7 1 → 1 → 1 → 1 → 1 → 1 → 1 1
9x mod 7 1 → 2 → 4 → 1 → 2 → 4 → 1 3
10x mod 7 1 → 3 → 2 → 6 → 4 → 5 → 1 6
11x mod 7 1 → 4 → 2 → 1 → 4 → 2 → 1 3
12x mod 7 1 → 5 → 4 → 6 → 2 → 3 → 1 6
EXAMPLE : With x0 = 2 , compute
xk = (137951 xk−1) mod 101 , k = 1, 2, · · · , 100 .
Result :
71 46 17 48 88 94 4 41 92 34
96 75 87 8 82 83 68 91 49 73
16 63 65 35 81 98 45 32 25 29
70 61 95 90 64 50 58 39 21 89
79 27 100 15 78 42 77 57 54 99
30 55 84 53 13 7 97 60 9 67
5 26 14 93 19 18 33 10 52 28
85 38 36 66 20 3 56 69 76 72
31 40 6 11 37 51 43 62 80 12
22 74 1 86 23 59 24 44 47 2
QUESTION : Are there repetitions (i.e., cycles) ?
EXAMPLE : As in the preceding example, use x0 = 2 , and
compute
xk = (137951 xk−1) mod 101 , k = 1, 2, · · · , 100 ,
and set
x̂k =
xk
100
.
0.710 0.460 0.170 0.480 0.880 0.940 0.040 0.410 0.920 0.340
0.960 0.750 0.870 0.080 0.820 0.830 0.680 0.910 0.490 0.730
0.160 0.630 0.650 0.350 0.810 0.980 0.450 0.320 0.250 0.290
0.700 0.610 0.950 0.900 0.640 0.500 0.580 0.390 0.210 0.890
0.790 0.270 1.000 0.150 0.780 0.420 0.770 0.570 0.540 0.990
0.300 0.550 0.840 0.530 0.130 0.070 0.970 0.600 0.090 0.670
0.050 0.260 0.140 0.930 0.190 0.180 0.330 0.100 0.520 0.280
0.850 0.380 0.360 0.660 0.200 0.030 0.560 0.690 0.760 0.720
0.310 0.400 0.060 0.110 0.370 0.510 0.430 0.620 0.800 0.120
0.220 0.740 0.010 0.860 0.230 0.590 0.240 0.440 0.470 0.020
QUESTION : Do these numbers look uniformly distributed ?
EXAMPLE : With x0 = 2 , compute
xk = (137953 xk−1) mod 101 , k = 1, 2, · · · , 100 .
Result :
75 35 50 57 67 38 11 59 41 73
61 15 7 10 72 74 48 83 32 89
55 93 3 62 2 75 35 50 57 67
38 11 59 41 73 61 15 7 10 72
74 48 83 32 89 55 93 3 62 2
75 35 50 57 67 38 11 59 41 73
61 15 7 10 72 74 48 83 32 89
55 93 3 62 2 75 35 50 57 67
38 11 59 41 73 61 15 7 10 72
74 48 83 32 89 55 93 3 62 2
QUESTION : Are there cycles ?
EXAMPLE : With x0 = 4 , compute
xk = (137953 xk−1) mod 101 , k = 1, 2, · · · , 100 .
Result :
49 70 100 13 33 76 22 17 82 45
21 30 14 20 43 47 96 65 64 77
9 85 6 23 4 49 70 100 13 33
76 22 17 82 45 21 30 14 20 43
47 96 65 64 77 9 85 6 23 4
49 70 100 13 33 76 22 17 82 45
21 30 14 20 43 47 96 65 64 77
9 85 6 23 4 49 70 100 13 33
76 22 17 82 45 21 30 14 20 43
47 96 65 64 77 9 85 6 23 4
QUESTIONS :
• Are there cycles ?
• Is this the same cycle that we already found ?
Generating Random Numbers using the Inverse Method
• There are algorithms that generate uniform random numbers.
• These can be used to generate other random numbers.
• A simple method to do this is the Inverse Transform Method .
RECALL :
Let f(x) be a density function on an interval [a, b] .
The distribution function is
F(x) ≡
Z x
a
f(x) dx .
• Since f(x) ≥ 0 we know that F(x) is increasing .
• If F(x) is strictly increasing then F(x) is invertible .
X x1 x2
We want random numbers X with distribution F(x) (blue) .
Let the random variable Y be uniform on the interval [0, 1] .
Let X = F−1
(Y ) .
Then P(x1 ≤ X ≤ x2) = y2 − y1 = F(x2) − F(x1) .
Thus F(x) is indeed the distribution function of X !
• If Y is uniformly distributed on [0, 1] then
P( y1 ≤ Y ≤ y2 ) = y2 − y1 .
• Let
X = F−1
(Y ) ,
with
x1 = F−1
(y1) and x2 = F−1
(y2) .
• Then
P(x1 ≤ X ≤ x2) = y2−y1 = F(x2)−F(x1) . ( Why ? )
• Thus F(X) is also the distribution function of X ≡ F−1
(Y ) !
NOTE : In the illustration X is on [0, 1] , but this is not necessary.
EXAMPLE :
Recall that the exponential density function
f(x) =



λe−λx
, x > 0
0 , x ≤ 0
has distribution function
F(x) =



1 − e−λx
, x > 0
0 , x ≤ 0
The inverse distribution function is
F−1
(y) = −
1
λ
log(1 − y) , 0 ≤ y < 1 . ( Check ! )
0 1 2 3 4 5 6
x
0.0
0.2
0.4
0.6
0.8
F
(x)
The inverse method for generating 20 ”exponential random numbers ” ,
for the exponential distribution function with λ = 1 .
EXAMPLE : ( continued · · · )
(Using the inverse method to get exponential random numbers.)
• Divide [0, 12] into 100 subintervals of equal size ∆x = 0.12 .
• Let Ik denote the kth interval, with midpoint xk .
• Use the inverse method to get N exponential random numbers.
• Let mk be the frequency count (# of random values in Ik) .
• Let ˆ
f(xk) =
mk
N ∆x
.
• Then
Z ∞
0
ˆ
f(x) dx ∼
=
100
X
k=1
ˆ
f(xk) ∆x = 1 .
• Then ˆ
f(x) approximates the actual density function f(x) .
0 1 2 3 4 5 6 7
x
0.0
0.2
0.4
0.6
0.8
1.0
f
(x)
0 1 2 3 4 5 6 7
x
0.0
0.2
0.4
0.6
0.8
1.0
f
(x)
N = 500 . N = 50, 000 .
Simulating the exponential random variable with λ = 1 .
(The actual density function is shown in blue.)
EXERCISE : Consider the Tent density function
f(x) =



x + 1 , −1 < x ≤ 0
1 − x , 0 < x ≤ 1
0 , otherwise
• Verify that that the distribution function is given by
F(x) =



1
2
x2
+ x + 1
2
, −1 ≤ x ≤ 0
−1
2
x2
+ x + 1
2
, 0 < x ≤ 1
• Verify that that the inverse distribution function is
F−1
(y) =



−1 +
√
2y , 0 ≤ y ≤ 1
2
1 −
√
2 − 2y , 1
2
< y ≤ 1
• Use the inverse method to generate ”Tent random numbers ”.
−1.0 −0.5 0.0 0.5 1.0
x
0.0
0.2
0.4
0.6
0.8
1.0
f
(x)
−1.0 −0.5 0.0 0.5 1.0
x
0.0
0.2
0.4
0.6
0.8
F
(x)
The ”Tent ” density Function. The ”Tent ” distribution function.
The inverse method for generating 20 ”Tent random numbers ” .
−1.00 −0.75 −0.50 −0.25 0.00 0.25 0.50 0.75 1.00
x
0.00
0.25
0.50
0.75
1.00
1.25
1.50
f
(x)
−1.00 −0.75 −0.50 −0.25 0.00 0.25 0.50 0.75 1.00
x
0.00
0.25
0.50
0.75
1.00
1.25
f
(x)
N = 500 . N = 50, 000 .
Simulating the ”Tent random variable ” .
(The actual density function is shown in blue.)

More Related Content

PDF
Intro probability 4
PPT
Clase3 Notacion
PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
PDF
Mathematics Ist year pdf.pdf
PDF
Maths 11
PPT
2010 3-24 cryptography stamatiou
PDF
Rosen, K. - Elementary Number Theory and Its Application (Instructor's Soluti...
PDF
Maths sa 1 synopsis
Intro probability 4
Clase3 Notacion
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Mathematics Ist year pdf.pdf
Maths 11
2010 3-24 cryptography stamatiou
Rosen, K. - Elementary Number Theory and Its Application (Instructor's Soluti...
Maths sa 1 synopsis

Similar to RANDOM NUMBER GENERATION, The Logistic Equation (20)

DOCX
DIGITAL TEXT BOOK
PDF
Appendex
DOCX
Rajnifile
PDF
Power Full Exposition
PPT
Design and analysis of algorithm ppt ppt
ODP
When Computers Don't Compute and Other Fun with Numbers
PPT
ch08 modified.pptmodified.pptmodified.ppt
PPT
lecture07 dicrete mathematics relation .ppt
DOCX
Theoryofcomp science
PDF
Computer science-formulas
PDF
Roots equations
PDF
Roots equations
PDF
Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
PPTX
Number System2.pptx
PPTX
The Most Beautiful formula
PDF
Advanced Calculus and Analysis MA1002 - CiteSeer ( PDFDrive ).pdf
PDF
Analysis of Algorithms - 2
PDF
Study on a class of recursive functions
PPT
ADVANCED ALGORITHMS-UNIT-3-Final.ppt
PDF
RSA Cryptosystem
DIGITAL TEXT BOOK
Appendex
Rajnifile
Power Full Exposition
Design and analysis of algorithm ppt ppt
When Computers Don't Compute and Other Fun with Numbers
ch08 modified.pptmodified.pptmodified.ppt
lecture07 dicrete mathematics relation .ppt
Theoryofcomp science
Computer science-formulas
Roots equations
Roots equations
Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
Number System2.pptx
The Most Beautiful formula
Advanced Calculus and Analysis MA1002 - CiteSeer ( PDFDrive ).pdf
Analysis of Algorithms - 2
Study on a class of recursive functions
ADVANCED ALGORITHMS-UNIT-3-Final.ppt
RSA Cryptosystem
Ad

Recently uploaded (20)

PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Introduction to machine learning and Linear Models
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Business Analytics and business intelligence.pdf
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PDF
Foundation of Data Science unit number two notes
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Lecture1 pattern recognition............
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Database Infoormation System (DBIS).pptx
climate analysis of Dhaka ,Banglades.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Introduction to machine learning and Linear Models
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
.pdf is not working space design for the following data for the following dat...
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Business Analytics and business intelligence.pdf
Introduction-to-Cloud-ComputingFinal.pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
1_Introduction to advance data techniques.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Foundation of Data Science unit number two notes
Reliability_Chapter_ presentation 1221.5784
Lecture1 pattern recognition............
Galatica Smart Energy Infrastructure Startup Pitch Deck
Ad

RANDOM NUMBER GENERATION, The Logistic Equation

  • 1. RANDOM NUMBER GENERATION • Measured data often have random fluctuations . • This may be due to inaccurate measurements . • It may also be due to other external influences . • Often we know or believe there is a deterministic model . • (i.e., the process can be modeled by a deterministic equation .) • However, deterministic equations can also have random behavior ! • The study of such equations is sometimes called chaos theory . • We will look at a simple example, namely, the logistic equation .
  • 2. The Logistic Equation A simple deterministic model of population growth is xk+1 = λ xk , k = 1, 2, · · · , for given λ , (λ ≥ 0) , and for given x0 , (x0 ≥ 0) . The solution is xk = λk x0 , k = 1, 2, · · · . Thus If 0 ≤ λ < 1 then xk → 0 as k → ∞ (”extinction”) . If λ > 1 then xk → ∞ as k → ∞ (”exponential growth”) .
  • 3. A somewhat more realistic population growth model is xk+1 = λ xk (1 − xk) , k = 1, 2, · · · , • This model is known as the logistic equation . • The maximum sustainable population is 1 . • λ is given, (0 ≤ λ ≤ 4) . • x0 is given, (0 ≤ x0 ≤ 1) . • Then 0 ≤ xk ≤ 1 for all k . (Prove this !) QUESTION : How does the sequence {xk}∞ k=1 depend on λ ?
  • 4. EXERCISE : xk+1 = λ xk (1 − xk) , k = 1, 2, · · · , • Divide the interval [0, 1] into 200 subintervals . • Compute xk for k = 1, 2, · · · 50 . • Count the xk’s in each subinterval. • Determine the percentage of xk’s in each subinterval. • Present the result in a diagram . • Do this for different choices of λ (0 ≤ λ ≤ 4) .
  • 5. 0.0 0.2 0.4 0.6 0.8 x 0.0 0.1 0.2 0.3 0.4 0.5 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 0.9 , x0 = 0.77 , 50 iterations , 200 subintervals .
  • 6. 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 1.7 , x0 = 0.77 , 50 iterations , 200 subintervals .
  • 7. 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.00 0.01 0.02 0.03 0.04 0.05 0.06 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.77 , x0 = 0.77 , 50 iterations , 200 subintervals .
  • 8. EXERCISE : xk+1 = λ xk (1 − xk) , k = 1, 2, · · · , Do the same as in the preceding example, but now • Compute xk for k = 1, 2, · · · 1, 000, 000 ! • Do not record the first 200 iterations. • ( This to eliminate transient effects .) • You will see that there is a fixed point (a cycle of period 1 ) .
  • 9. 0.0 0.2 0.4 0.6 0.8 1.0 x 0.0 0.2 0.4 0.6 0.8 1.0 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 1.7 , 1, 000, 000 iterations , 200 subintervals . There is a fixed point (a cycle of period 1 ) .
  • 10. 0.0 0.2 0.4 0.6 0.8 1.0 x 0.00 0.05 0.10 0.15 0.20 0.25 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.46 , 1, 000, 000 iterations , 200 subintervals . There is a cycle of period 4 .
  • 11. 0.0 0.2 0.4 0.6 0.8 1.0 x 0.00 0.02 0.04 0.06 0.08 0.10 0.12 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.561 , 1, 000, 000 iterations , 200 subintervals . There is a cycle of period 8 .
  • 12. 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.6 , 1, 000, 000 iterations , 200 subintervals . (The Figure on the right only shows 100 of these iterations.) There is apparent chaotic behavior .
  • 13. 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.000 0.005 0.010 0.015 0.020 0.025 0.030 0.035 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.77 , 1, 000, 000 iterations , 200 subintervals . (The Figure on the right only shows 100 of these iterations.) There is apparent chaotic behavior .
  • 14. 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.000 0.005 0.010 0.015 0.020 0.025 0.030 0.035 0.040 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.89 , 1, 000, 000 iterations , 200 subintervals . (The Figure on the right only shows 100 of these iterations.) There is apparent chaotic behavior .
  • 15. 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.000 0.005 0.010 0.015 0.020 0.025 0.030 0.035 p(x) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 y Percentage per interval Graphical interpretation. λ = 3.99 , 1, 000, 000 iterations , 200 subintervals . (The Figure on the right only shows 100 of these iterations.) There is apparent chaotic behavior .
  • 16. CONCLUSIONS : • The behavior of the logistic equation depends on λ . • For certain values of λ we see fixed points . • For other values of λ there are cycles . • For yet other values of λ there is seemingly random behavior . • Many other deterministic equations have ”complex behavior ” ! • Nature is complex !
  • 17. Generating Random Numbers The logistic equation is a recurrence relation of the form xk+1 = f(xk) , k = 1, 2, 3, · · · . Most random number generators also have this form. For the logistic equation we did not see sequences {xk}N k=1 having • a uniform distribution, • a normal distribution, • another known distribution. QUESTION : • How to generate uniform (and other) random numbers ? • ( These are useful in computer simulations .)
  • 18. Generating Uniformly Distributed Random Numbers Uniformly distributed random numbers can also be generated by a recurrence relation of the form xk+1 = f(xk) , k = 1, 2, 3, · · · . Unlike the logistic equation, the xk are most often integers . The recurrence relation typically has the form xk+1 = (n xk) mod p . where p is a large prime number , and n a large integer , with p 6 | n .
  • 19. The following fact is useful : THEOREM : Let p be a prime number, and n an integer such that p 6 | n . Then the function f : { 0, 1, 2, · · · , p − 1 } → { 0, 1, 2, · · ·, p − 1 } , given by f(x) = (n x) mod p , is one-to-one (and hence onto, a bijection, and invertible).
  • 20. p prime , p 6 | n ⇒ (n x) mod p is 1 − 1 EXAMPLE : p = 7 and n = 12 . x 12x 12x mod 7 0 0 0 1 12 5 2 24 3 3 36 1 4 48 6 5 60 4 6 72 2 Invertible ! NOTE : The values of 12x mod 7 look somewhat random !
  • 21. p prime , p 6 | n ⇒ (n x) mod p is 1 − 1 EXAMPLE : p = 6 and n = 2 . x 2x 2x mod 6 0 0 0 1 2 2 2 4 4 3 6 0 4 8 2 5 10 4 Not Invertible .
  • 22. p prime , p 6 | n ⇒ (n x) mod p is 1 − 1 EXAMPLE : p = 6 and n = 13 . x 13x 13x mod 6 0 0 0 1 13 1 2 26 2 3 39 3 4 52 4 5 65 5 Invertible . ( So ? ) NOTE : The numbers in the right hand column don’t look random !
  • 23. p prime , p 6 | n ⇒ (n x) mod p is 1 − 1 PROOF : By contradiction : Suppose the function is not 1 − 1 . Then there are distinct integers x1 , x2 ∈ { 0, 1, 2, · · ·, p − 1 } , such that (nx1) mod p = k and (nx2) mod p = k , where also k ∈ { 0, 1, 2, · · ·, p − 1 } . It follows that p | n(x1 − x2) , ( Why ? ) Since p is prime and p 6 | n it follows that p | (x1 − x2) . Thus x1 −x2 = 0 ( Why ? ) , i.e. , x1 = x2 . Contradiction !
  • 24. For given x0 , an iteration of the form xk = (n xk−1) mod p , k = 1, 2, · · · , p − 1 . can be used to generate random numbers . • Here p is a large prime number . • The value of n is also large . • The integer n must not be divisible by p . • Do not start with x0 = 0 (because it is a fixed point !) . • Be aware of cycles (of period less than p − 1) ! REMARK : Actually, more often used is an iteration of the form xk = (n xk−1 + m) mod p , k = 1, 2, · · · , p − 1 .
  • 25. EXAMPLE : As a simple example, take again p = 7 and n = 12 : x 12x 12x mod 7 0 0 0 1 12 5 2 24 3 3 36 1 4 48 6 5 60 4 6 72 2 With x1 = 1 the recurrence relation xk+1 = f(xk) , where f(x) = 12x mod 7 , generates the sequence 1 → 5 → 4 → 6 → 2 → 3 → 1 → 5 → · · · which is a cycle of maximal period p − 1 (here 6) .
  • 26. f(x) sequence cycle period 5x mod 7 1 → 5 → 4 → 6 → 2 → 3 → 1 6 6x mod 7 1 → 6 → 1 → 6 → 1 → 6 → 1 2 8x mod 7 1 → 1 → 1 → 1 → 1 → 1 → 1 1 9x mod 7 1 → 2 → 4 → 1 → 2 → 4 → 1 3 10x mod 7 1 → 3 → 2 → 6 → 4 → 5 → 1 6 11x mod 7 1 → 4 → 2 → 1 → 4 → 2 → 1 3 12x mod 7 1 → 5 → 4 → 6 → 2 → 3 → 1 6
  • 27. EXAMPLE : With x0 = 2 , compute xk = (137951 xk−1) mod 101 , k = 1, 2, · · · , 100 . Result : 71 46 17 48 88 94 4 41 92 34 96 75 87 8 82 83 68 91 49 73 16 63 65 35 81 98 45 32 25 29 70 61 95 90 64 50 58 39 21 89 79 27 100 15 78 42 77 57 54 99 30 55 84 53 13 7 97 60 9 67 5 26 14 93 19 18 33 10 52 28 85 38 36 66 20 3 56 69 76 72 31 40 6 11 37 51 43 62 80 12 22 74 1 86 23 59 24 44 47 2 QUESTION : Are there repetitions (i.e., cycles) ?
  • 28. EXAMPLE : As in the preceding example, use x0 = 2 , and compute xk = (137951 xk−1) mod 101 , k = 1, 2, · · · , 100 , and set x̂k = xk 100 . 0.710 0.460 0.170 0.480 0.880 0.940 0.040 0.410 0.920 0.340 0.960 0.750 0.870 0.080 0.820 0.830 0.680 0.910 0.490 0.730 0.160 0.630 0.650 0.350 0.810 0.980 0.450 0.320 0.250 0.290 0.700 0.610 0.950 0.900 0.640 0.500 0.580 0.390 0.210 0.890 0.790 0.270 1.000 0.150 0.780 0.420 0.770 0.570 0.540 0.990 0.300 0.550 0.840 0.530 0.130 0.070 0.970 0.600 0.090 0.670 0.050 0.260 0.140 0.930 0.190 0.180 0.330 0.100 0.520 0.280 0.850 0.380 0.360 0.660 0.200 0.030 0.560 0.690 0.760 0.720 0.310 0.400 0.060 0.110 0.370 0.510 0.430 0.620 0.800 0.120 0.220 0.740 0.010 0.860 0.230 0.590 0.240 0.440 0.470 0.020 QUESTION : Do these numbers look uniformly distributed ?
  • 29. EXAMPLE : With x0 = 2 , compute xk = (137953 xk−1) mod 101 , k = 1, 2, · · · , 100 . Result : 75 35 50 57 67 38 11 59 41 73 61 15 7 10 72 74 48 83 32 89 55 93 3 62 2 75 35 50 57 67 38 11 59 41 73 61 15 7 10 72 74 48 83 32 89 55 93 3 62 2 75 35 50 57 67 38 11 59 41 73 61 15 7 10 72 74 48 83 32 89 55 93 3 62 2 75 35 50 57 67 38 11 59 41 73 61 15 7 10 72 74 48 83 32 89 55 93 3 62 2 QUESTION : Are there cycles ?
  • 30. EXAMPLE : With x0 = 4 , compute xk = (137953 xk−1) mod 101 , k = 1, 2, · · · , 100 . Result : 49 70 100 13 33 76 22 17 82 45 21 30 14 20 43 47 96 65 64 77 9 85 6 23 4 49 70 100 13 33 76 22 17 82 45 21 30 14 20 43 47 96 65 64 77 9 85 6 23 4 49 70 100 13 33 76 22 17 82 45 21 30 14 20 43 47 96 65 64 77 9 85 6 23 4 49 70 100 13 33 76 22 17 82 45 21 30 14 20 43 47 96 65 64 77 9 85 6 23 4 QUESTIONS : • Are there cycles ? • Is this the same cycle that we already found ?
  • 31. Generating Random Numbers using the Inverse Method • There are algorithms that generate uniform random numbers. • These can be used to generate other random numbers. • A simple method to do this is the Inverse Transform Method .
  • 32. RECALL : Let f(x) be a density function on an interval [a, b] . The distribution function is F(x) ≡ Z x a f(x) dx . • Since f(x) ≥ 0 we know that F(x) is increasing . • If F(x) is strictly increasing then F(x) is invertible .
  • 33. X x1 x2 We want random numbers X with distribution F(x) (blue) . Let the random variable Y be uniform on the interval [0, 1] . Let X = F−1 (Y ) . Then P(x1 ≤ X ≤ x2) = y2 − y1 = F(x2) − F(x1) . Thus F(x) is indeed the distribution function of X !
  • 34. • If Y is uniformly distributed on [0, 1] then P( y1 ≤ Y ≤ y2 ) = y2 − y1 . • Let X = F−1 (Y ) , with x1 = F−1 (y1) and x2 = F−1 (y2) . • Then P(x1 ≤ X ≤ x2) = y2−y1 = F(x2)−F(x1) . ( Why ? ) • Thus F(X) is also the distribution function of X ≡ F−1 (Y ) ! NOTE : In the illustration X is on [0, 1] , but this is not necessary.
  • 35. EXAMPLE : Recall that the exponential density function f(x) =    λe−λx , x > 0 0 , x ≤ 0 has distribution function F(x) =    1 − e−λx , x > 0 0 , x ≤ 0 The inverse distribution function is F−1 (y) = − 1 λ log(1 − y) , 0 ≤ y < 1 . ( Check ! )
  • 36. 0 1 2 3 4 5 6 x 0.0 0.2 0.4 0.6 0.8 F (x) The inverse method for generating 20 ”exponential random numbers ” , for the exponential distribution function with λ = 1 .
  • 37. EXAMPLE : ( continued · · · ) (Using the inverse method to get exponential random numbers.) • Divide [0, 12] into 100 subintervals of equal size ∆x = 0.12 . • Let Ik denote the kth interval, with midpoint xk . • Use the inverse method to get N exponential random numbers. • Let mk be the frequency count (# of random values in Ik) . • Let ˆ f(xk) = mk N ∆x . • Then Z ∞ 0 ˆ f(x) dx ∼ = 100 X k=1 ˆ f(xk) ∆x = 1 . • Then ˆ f(x) approximates the actual density function f(x) .
  • 38. 0 1 2 3 4 5 6 7 x 0.0 0.2 0.4 0.6 0.8 1.0 f (x) 0 1 2 3 4 5 6 7 x 0.0 0.2 0.4 0.6 0.8 1.0 f (x) N = 500 . N = 50, 000 . Simulating the exponential random variable with λ = 1 . (The actual density function is shown in blue.)
  • 39. EXERCISE : Consider the Tent density function f(x) =    x + 1 , −1 < x ≤ 0 1 − x , 0 < x ≤ 1 0 , otherwise • Verify that that the distribution function is given by F(x) =    1 2 x2 + x + 1 2 , −1 ≤ x ≤ 0 −1 2 x2 + x + 1 2 , 0 < x ≤ 1 • Verify that that the inverse distribution function is F−1 (y) =    −1 + √ 2y , 0 ≤ y ≤ 1 2 1 − √ 2 − 2y , 1 2 < y ≤ 1 • Use the inverse method to generate ”Tent random numbers ”.
  • 40. −1.0 −0.5 0.0 0.5 1.0 x 0.0 0.2 0.4 0.6 0.8 1.0 f (x) −1.0 −0.5 0.0 0.5 1.0 x 0.0 0.2 0.4 0.6 0.8 F (x) The ”Tent ” density Function. The ”Tent ” distribution function. The inverse method for generating 20 ”Tent random numbers ” .
  • 41. −1.00 −0.75 −0.50 −0.25 0.00 0.25 0.50 0.75 1.00 x 0.00 0.25 0.50 0.75 1.00 1.25 1.50 f (x) −1.00 −0.75 −0.50 −0.25 0.00 0.25 0.50 0.75 1.00 x 0.00 0.25 0.50 0.75 1.00 1.25 f (x) N = 500 . N = 50, 000 . Simulating the ”Tent random variable ” . (The actual density function is shown in blue.)