Random Number Variate Generation
Random Number Variate Generation
EMÜ322
Simulation Modeling and Analysis
Random Number & Random Variate
Generation
Banu Yuksel Ozkaya
Hacettepe University
Department of Industrial Engineering
1
1
4/2/2024
2
4/2/2024
3
4/2/2024
4
4/2/2024
5
4/2/2024
11
6
4/2/2024
13
14
7
4/2/2024
15
8
4/2/2024
17
18
9
4/2/2024
19
20
10
4/2/2024
21
0.9
1000
0.8
0.7
800
0.6
i+1
0.5 600
U
0.4
400
0.3
0.2
200
0.1
0 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Ui
11
4/2/2024
23
24
12
4/2/2024
25
13
4/2/2024
tests
28
14
4/2/2024
29
30
15
4/2/2024
k
c 02 =
(Oi − Ei )2
i =1 Ei
31
32
16
4/2/2024
33
34
17
4/2/2024
36
18
4/2/2024
cent interval
38
19
4/2/2024
39
20
4/2/2024
41
D = max F ( x) − S n ( x)
42
21
4/2/2024
– Compute D=max(D+,D-)
– If D>Da,n , reject H0. If D≤Da,n , conclude that no
difference has been detected between the true distribution
of U1,U2… Un and the uniform distribution.
43
44
22
4/2/2024
45
46
23
4/2/2024
48
24
4/2/2024
50
25
4/2/2024
1.0
0.8
0.6
0.4
Autocorrelation
0.2
0.0
-0.2
-0.4
-0.6
-0.8
-1.0
1 2 3 4 5 6 7 8
Lag
51
52
26
4/2/2024
1.0
0.8
0.6
0.4
Autocorrelation
0.2
0.0
-0.2
-0.4
-0.6
-0.8
-1.0
27
4/2/2024
55
56
28
4/2/2024
57
58
29
4/2/2024
__
x
60
30
4/2/2024
61
Random-Variate Generation
• Generation of a random-variate refers to the
activity of obtaining an observation on a
random variable from the desired distribution.
• It is now assumed that a distribution has been
specified somehow (distribution fitting) and we
address the issue of how we can generate the
random-variates with this distribution in order
to run the simulation model.
62
31
4/2/2024
Random-Variate Generation
• Example:
– Inter-arrival and service time in the single-server
queuing system
– Inter-arrival, loading, weighing and travel times in
the truck-load system
were needed for simulation of the corresponding
system.
63
Random-Variate Generation
• The basic ingredient needed for every method
of generating random variates from any
distribution or random process is a source of
independent and identically distributed U(0,1)
random variates (random numbers).
• Assume that a good source of random numbers
is available for use.
64
32
4/2/2024
Random-Variate Generation
• Factors to choose an algorithm to use:
– Exactness:
• Result in random variates with exactly the desired
distribution (within the limits of the machine accuracy)
– Efficiency:
• Use less storage space and execution time.
– Complexity:
• Overall complexity of the algorithm such as the
conceptual and implementation factors.
– Robustness:
• Efficient for all parameter values
65
Random-Variate Generation
• General Approaches:
– Inverse Transform
– Composition
– Convolution
– Acceptance-Rejection
66
33
4/2/2024
Random-Variate Generation
• Inverse-Transform Technique: Algorithm for
generating a continuous random-variate X
having distribution function F() is as follows:
1. Generate U~ U(0,1).
2. Compute X= F-1(U) (X s.t. F(X)=U).
F(x)
U
X
67
68
34
4/2/2024
Random-Variate Generation
• Inverse-Transform Technique:
– Example: How to generate an exponential random-
variate with parameter l?
1 − e − lx x0
F (x ) =
0 o.w.
1. Generate U~ U(0,1).
2. Compute X s.t. U=F(X)=1-e-lX ➔ X=-ln(1-U)/l.
(or X= -ln(U)/l)
69
70
35
4/2/2024
Random-Variate Generation
• Inverse-Transform Technique:
– Example: Generate 10000 exponential random
variates with l=2.
1200 3500
3000
1000
2500
800
2000
Frequency
600
1500
400
1000
200
500
0 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 1 2 3 4 5 6
X
– mean=0.5032, variance=0.2540 71
Random-Variate Generation
• Inverse-Transform Technique: Why does the
Inverse Transform Technique work?
Exponential density (l=2) Exponential distribution function (l=2)
2 1
1.8 0.9
1.6 0.8
1.4 0.7
1.2 0.6
F(x)
f(x)
1 0.5
0.8 0.4
0.6 0.3
0.4 0.2
0.2 0.1
0 0
0 0.5 1 1.5 2 2.5 3 3.5 4 0 0.5 1 1.5 2 2.5 3 3.5 4
x x
72
36
4/2/2024
Random-Variate Generation
• Example: Consider a random variable X that
has the following probability density function:
x 0 x 1
f ( x ) = 2 − x 1 x 2
0
o.w.
73
f(x)
x
74
37
4/2/2024
75
76
38
4/2/2024
Random-Variate Generation
• Inverse-Transform Method: Algorithm for
generating a discrete random-variate X taking
values x1,x2,…. where x1<x2<… and having
distribution function F() is as follows:
1. Generate U~ U(0,1).
2. Determine the smallest positive integer i such that
U≤ F(xi) and return X= xi.
77
78
39
4/2/2024
Random-Variate Generation
• Inverse-Transform Method:
1
X
0 X X
79
Random-Variate Generation
• Inverse-Transform Method:
• The value X returned by this technique has the
desired distribution F.
– i=1 ➔ X=x1 ↔ U ≤ F(x1) = p(x1)
– i≥2 ➔ X=xi ↔ F(xi-1) < U ≤ F(xi) →
P(X=xi)=P(F(xi-1) < U ≤ F(xi))= F(xi) - F(xi-1)= p(xi)
80
40
4/2/2024
(Slide #54)
81
Random-Variate Generation
• Inverse-Transform Method:
– Example: Generate 10000 geometric random
variates with p=1/5.
1200 4000
3500
1000
3000
800
2500
Frequency
600 2000
1500
400
1000
200
500
0 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 5 10 15 20 25 30 35 40 45 50
X
– mean=5.0280, variance=20.3314 82
41
4/2/2024
Random-Variate Generation
• Inverse-Transform Method:
– Example: In an inventory system, the demand sizes
are random taking on the values 1,2,3, and 4 with
respective probabilities 1/6, 1/3, 1/3 and 1/6.
83
F(x)
x
84
42
4/2/2024
Random-Variate Generation
• Inverse-Transform Method:
– Example:
1200 3500
3000
1000
2500
800
2000
600
1500
400
1000
200
500
0 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1 2 3 4
– mean=2.5035, variance=0.9219
85
Random-Variate Generation
• Advantages of the Inverse Transform Method
1. Both valid to generate continuous and discrete
random variables➔ valid to generate mixed random
variables
X = min x : F ( x ) U
86
43
4/2/2024
Random-Variate Generation
• Advantages of the Inverse Transform Method
2. Can be used to generate correlated random
variables
• Positive correlation
• Negative correlation.
87
88
44
4/2/2024
Random-Variate Generation
• Disadvantages of the Inverse Transform
Method
– The need to calculate F-1(U) or F(U).
• Normal, gamma, beta distribution
– Usually not the fastest way to generate a random
variate.
89
Random-Variate Generation
• Convolution Method:
– A random variable X can be expressed as a sum of other
random variables that are independent and identically
distributed.
X = Y1 + Y2 + ....Ym
• Gamma (Erlang) random variable
• Binomial random variable
• Negative Binomial random variable
– X is said to be a m-fold convolution of the distribution of
Yj’s.
– Rather than generating X directly, generate m Y’s and
then sum them up.
90
45
4/2/2024
91
Random-Variate Generation
• Composition Method:
– When the distribution function F from which we
wish to generate can be expressed as a convex
combination of other distribution functions, the
composition technique works.
– Suppose we assume that for all x, F(x) can be written
as:
F ( x ) = p j F j ( x)
j =1
where Spj=1. Then,
f ( x ) = p j f j ( x)
j =1
92
46
4/2/2024
93
(Slide #61)
94
47
4/2/2024
Random-Variate Generation
• Composition Method:
1. Generate a positive random integer J, such that
P(J = j ) = p j
for j = 1,2,.....
Random-Variate Generation
• Composition Method:
• Example: The double exponential (or Laplace)
distribution has density as follows:
f (x ) = 0.5e x I ( −,0) ( x) + 0.5e − x I [0, ) ( x)
96
48
4/2/2024
Random-Variate Generation
• Composition Method:
• Example:
– Generate U1,U2.
– If U1<0.5, X=ln(U2)
– If U1>0.5, X=-ln(U2)
97
98
49
4/2/2024
99
100
50
4/2/2024
101
Random-Variate Generation
• Composition Method:
• Example: For 0<a<1, the right-trapezoidal
distribution has density as follows:
a + 2(1 − a) x 0 x 1
f (x ) =
0 o.w.
102
51
4/2/2024
Random-Variate Generation
• Composition Method:
• Example:
– Generate U1,U2.
– If U1<=a, X=U2
– If U1>a, X=sqrt(U2)
103
104
52
4/2/2024
105
106
53
4/2/2024
Random-Variate Generation
• Acceptance-Rejection Method:
– Other three methods are direct in the sense that
they deal directly with the distribution or random
variable desired.
– Acceptance-rejection region is useful when direct
methods fail or are inefficient.
– Aim is to generate a continuous random variable X
with distribution function F and density f.
107
108
54
4/2/2024
Random-Variate Generation
• Acceptance-Rejection Method:
– Specify a function t, which is called the majorizing function
such that t(x)>=f(x) for all x
109
110
55
4/2/2024
Random-Variate Generation
• Acceptance-Rejection Method:
– For the majorizing function such that t(x)>=f(x) for
all x
c = t ( x)dx f ( x)dx = 1
− −
111
Random-Variate Generation
• Acceptance-Rejection Method:
1. Generate Y having density r.
2. Generate U independent of Y.
3. If U<=f(Y)/t(Y), return X=Y (accept the random
variate Y as X).
Otherwise (reject the random variate Y) go to step
1 and try again.
112
56
4/2/2024
113
a b
more frequently
114
57
4/2/2024
Random-Variate Generation
• Acceptance-Rejection Method:
Example: Suppose we want to generate a beta
random variable with parameters 4 and 3.
60 x 3 (1 − x ) 2
0 x 1
f ( x) =
0 o.w.
115
Random-Variate Generation
• Acceptance-Rejection Method:
Example: The maximum value of f(x) occurs at
x=0.6 with f(0.6)=2.0736.
2.5
t(x)=2.0736
f(x)
1.5
r(x)=1
1
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
116
58
4/2/2024
Random-Variate Generation
• Acceptance-Rejection Method:
Example: Define t(x) as follows:
2.0736 0 x 1
t ( x) =
0 o.w.
c=2.0736. Then, r(x) is given by
1 0 x 1
r ( x) =
0 o.w.
117
Random-Variate Generation
• Acceptance-Rejection Method:
Example:
1. Generate Y with density r(x).
2. Generate U independent of Y.
60Y 3 (1 − Y )
2
3. If U , return X=Y.
2.0736
Otherwise go to step 1 again.
118
59
4/2/2024
119
60
4/2/2024
Y=0.28
U=0.84
121
122
61
4/2/2024
123
In this example,
700
600
500
100
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
124
62
4/2/2024
Random-Variate Generation
• Acceptance-Rejection Method:
Example:
1200 700
600
1000
500
800
400
600
300
400
200
200
100
0 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
125
Random-Variate Generation
• Acceptance-Rejection Method:
– The method generates a random number with the
desired distribution regardless of the choice of the
majorizing function, t(x).
– The choice of the majorizing function is important
for the efficiency of the algorithm.
• Step 1 requires generating Y with density t(x)/c.
Therefore, we should choose so that this can be
accomplished rapidly.
• We want to have a majorizing function for which the
probability of rejection (acceptance) is very small (high).
126
63
4/2/2024
127
64