0% found this document useful (0 votes)
58 views31 pages

Simulasi Random

The document describes how to simulate random behavior by generating random numbers using a linear congruential generator (LCG). It provides an example of an LCG with a=21, c=3, m=16 and shows the calculation of random numbers Zi and uniform random variables Ui over multiple iterations. It also discusses using random number streams and the maximum cycle length that can be achieved with an LCG.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views31 pages

Simulasi Random

The document describes how to simulate random behavior by generating random numbers using a linear congruential generator (LCG). It provides an example of an LCG with a=21, c=3, m=16 and shows the calculation of random numbers Zi and uniform random variables Ui over multiple iterations. It also discusses using random number streams and the maximum cycle length that can be achieved with an LCG.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

4.

Simulating Random Behavior


Cara Menghasilkan Angka Acak

•Perilaku acak ditiru dalam simulasi dengan


menggunakan generator bilangan acak
•Bilangan yang dihasilkan oleh generator bilangan
acak dapat menghasilkan urutan bilangan yang sama
berulang kali  generator bilangan acak semu
How to Generate Random Numbers

The uniform(0,1) distribution of a random number generator


Linear Congruential Generators (LCG)

Metoda:

Barisan bilangan bulat Z1, Z2, Z3, … didefinisikan dengan


rumus

Zi = (aZi-1 + c) mod (m)

a : multiplier
c : increment
m: modulus

Modulus =sisa pembagian


Contoh : 11 mod(2) = bilangan bulat hasil bagi oleh
2 + sisa pembagian = 11/2=5+ sisa pembagian (1)
Pada formula excel :MOD(11,2)=1
LCG: Example

 a=21, c=3, m=16 untuk enghasilkan bilangan random semu (pseudo


random)
 Zi= (aZi-1 + c) mod(m)  Zi= (21Zi-1 + 3) mod (16)
 Z0 = 13 (dipilih secara senbarang antara 0 and 15 (m-1))  sebagai
nilai awal
 Z1 = (21Z0 + 3) mod (16) = (21(13)+3) mod (16) = 276 mod (16) = 4
 Ui = Zi/m= Zi/16 = 4/16 = 0.2500
Table 1 Example LCG
Zi = (21Zi-1 + 3) mod(16)
i 21Zi-1+3 Zi Ui=Zi/16
0   13  
1 276 4 0,25
2 87 7 0,4375
3 150 6 0,375
4 129 1 0,0625
5 24 8 0,5
6 171 11 0,6875
7 234 10 0,625
8 213 5 0,3125
9 108 12 0,75
10 255 15 0,9375
11 318 14 0,875
12 297 9 0,5625
13 192 0 0
14 3 3 0,1875
15 66 2 0,125
16 45 13 0,8125
17 276 4 0,25
18 87 7 0,4375
19 150 6 0,375
20 129 1 0,0625
LCG: example (cont)

We will execute five replications of a simulation. To run that


simulation model for one replication requires that the
random number generator be called 1,000 times during the
simulation.

We would need a random number generator with a cycle


length of at least 5,000
LCG (cont)

A guideline to select a, c and m to realize the


maximum cycle length :
• m = 2b, where b is determined based on the
number of bits per word on the computer being
used. Many computers use 32 bits per word,
making 31 a good choice for b
• c and m such that the greatest common factor is
1 (the only positive integer that exactly divides
both m and c is 1)
• a = 1 + 4k, where k is an integer
LCG (cont.)
• The maximum cycle length that an LCG can
achieve is m
• LCG can achieve a full cycle length of over 2.1
billion (231) random numbers
Example

Simulating “random” events in a drive-through


restaurant
• the arrival time of cars to a restaurant’s drive-
through window
• The time takes the driver to place an order
• The number of hamburgers, drinks, and fries
ordered
• The time it takes the restaurant to prepare the
order
Testing Random Number Generators

The numbers produced by the random number


generators must be
(1) independent and
(2) uniformly distributed between zero and one
(uniform (0,1)).
Stream
• The long sequence of random
numbers is subdivided into smaller
segments, as streams.
• i.e.: stream 1: arrival pattern of cars
to a restaurant’s drive-through
window,
• stream 2: time required for the driver
of the car to place and order
Mencatat nilai Zi yang menandai awal setiap

LCG: how to implement


• Tentukan berapa banyak pembangkit
bilangan random untuk ditempatkan pada
masing-masing streams
• Bagi lagi urutan pembangkit bilangan acak ke
dalam masing-masing streams
• Bangkitan seluruh urutan bilangan random
(cycle length)
• Catat nilai Zi yang menandai awal setiap
stream
• Setiap stream memiliki nilai awal sendiri
LCG (cont)
Two types of LCG:
• Mixed congruential generators: c>0
• Multiplicative congruential generators c=0
• More efficient than the mixed generator  no require the
addition of c

Promodel uses the multiplicative generator Zi= (630,360,016Zi-1)


mod(231-1)
How to generate random variates

How to generate observations (random variates) from


probability distribution other than the uniform (0,1)
distribution?

Transforming the observations generated by the random


number generator to the desired distribution

Transformed value  variates from the specified


distribution
How to generate random variates (cont.)

Types of method for generating random variates from


a desired distribution:
• Inverse transformation method
• The acceptance/rejection method
• The composition method
• The convolution method
• Methods employing special properties
Generating Random Variates
(Using inverse transformation)

1. Given a probability density function f(x).


2. Find the cumulative distribution function
of X, that is F(x) = P(X ≤ x).
3. Set U = F(x), where U is uniform(0,1).
4. Solving for x = F-1(U)
Continuous Distributions (an example: exponential
distribution with mean )

 1 x /  F(x)

 e for x  0
f ( x)    1.00 U=1 - exp(-x/beta)
0 elsewhere

U2
x / 
1  e for x  0
F ( x)  
0 elsewhere
0.50

U  1  e  x / U1

x   ln(1  U )

x1 x2

Graphical explanation of inverse transformation method for continuous variates


How to generate random variates (cont.)

Discrete distribution:
• Basically the same as for the continuous case
• Example : a simulation to represent the number of
defective components on a circuit board, number of
drinks ordered from a drive-through window
How to generate random variates (cont.)

Discrete distribution:
5. Simple Spreadsheet Simulation

An example: Dynamic, stochastic simulation model


Customers arrive to use an automatic teller machine (ATM) at a
mean inter-arrival time of 3.0 minutes exponentially
distributed. When customers arrive to the system they join
a queue to wait for their turn on the ATM. The queue has
the capacity to hold an infinity number of customers.
Customers spend an average of 2.4 minutes exponentially
distributed at the ATM to complete their transactions,
which is called the service time at the ATM.
• Simulate the system for the arrival and processing of 25
customers and
• estimate the expected waiting time for customers in the queue
(the average time customers wait in line for the ATM) and
• the expected time in the system (the average time customers
wait in the queue plus the average time it takes them to
complete their transaction at the ATM).
An example:
Dynamic, stochastic simulation model

• System: ATM
• Entities: customers that arrive to the ATM for
processing
• Resource: ATM that serves the customers with
the capacity to serve one customer at a time
• The system controls that dictate how, when and
were activities are performed for this ATM
system
Arriving customers ATM queue ATM server Departing
(entities) (FIFO) (resource) customers
(entities)

8 7 6 5 4 3 2 1

Interarrival
time 4.8
minutes

7th customer 6th customer


arrives at arrives at
21.0 min. 16.2 min.

Figure: Descriptive drawing of the ATM system


Simulating Random Variates
The transformation equation is:

Where
X    ln(1  U )
i i

Xi = the i th value from the exponential


distribution with mean ,
Ui = the ith random drawn from a
uniform(0,1) distribution
i = 1,2,…,25
Ui will be produced by the equation:

Z1i  (21Z1i 1  3) mod(128)


U 1i  Z1i / 128 i  1,2,..., 25
Z10  3
X 1i  3.0 ln(1  U 1i )

Z 2i  (21Z 2i 1  3) mod(128)
U 2i  Z 2i / 128 i  1,2,...,25
Z 20  122
X 2i  2.4 ln(1  U 2i )
Tabel. Summary of ATM System Simulation Output
Replication Average Time in Queu Average Time in System
1 1.94 minutes 4.26 minutes
2 0.84 minutes 2.36 minutes
Average 1.39 minutes 3.31 minutes

Replication Z10 Z20

1 3 122

2 29 92
• Changing the seed values Z10 and Z20 causes the
spreadsheet program to recompute all values in the
spreadsheet.
• When we change the seed values Z1 0 and Z20
appropriately, we produce another replication of
the simulation.
• The heart of simulation is the generation of the
random variates that drive the stochastic events in
the simulation.
Summary

Modeling random behavior:


• Transforming the output produced by a random number
generator into observation (random variates) from an
appropriate statistical distribution
• Combining with logical operators in a computer program to
compute output that mimics the performance behavior of
stochastic system
• Performance estimates for stochastic simulations are
obtained by calculating the average value of the
performance metric across several replications of the
simulation
• Models can realistically simulate a variety of systems
Review Questions

1. What is the difference between a stochastic and


a deterministic model in terms of the input
variables and the way results are interpreted?
2. Give a statistical description of the numbers
produced by a random number generator.
3. What are the two statistical properties that
random numbers must satisfy?
4. Given a LCG: Zi = (12Zi-1 + 5) mod(32)
Compute Z1 through Z5 from a seed of 29 (Z0 =
29).
5. What is a random variate, and how are random
variates generated?
7. Apply the inverse transformation method to generate three
variates from the following distributions using U1 = 0.10, U2 =
0.53, and U3 = 0.15.
 1 x
 for   x    for x  1,2,3,4,5
( a) f ( x)      (b) p ( x)  P ( X  x)   5
0 elsewhere 0 elsewhere

where   7 and   4.
8. Reproduce the spreadsheet simulation of the
ATM system presented in this week lecture.
Set the random numbers seeds Z10=29 and
Z20=92 to compute the average time customers
spend in the queue and in the system.

You might also like