Simulation and Modeling- Lecture 2
Simulation and Modeling- Lecture 2
Random Number
Where:
Example:
- a = 1103515245
- c = 12345
- m = 2^31
- Initial seed (X(0)) = 123456789
And so on...
1. Periodicity: LCGs have a periodic output, meaning that the sequence will
eventually repeat.
2. Uniform distribution: LCGs can produce uniformly distributed random
numbers, but this depends on the choice of parameters.
3. Determinism: LCGs are deterministic, meaning that given the same initial
seed and parameters, the same sequence of numbers will be generated.
Advantages:
1. Fast and efficient: LCGs are relatively fast and efficient compared to other
PRNGs.
2. Easy to implement: LCGs are simple to implement, requiring only basic
arithmetic operations.
Disadvantages:
What we usually do is to take for instance ten pieces of papers and number
them 0,1,2,3,4,5,6,7,8, and 9 , fold and place them in a box. Shake the box
and thoroughly mix the slips of paper. Select a slip; then record the number
that is on it. Replace the slip and repeat this procedure over and over. The
resultant record of digits is a realized sequence of random numbers.
Assuming you thoroughly mix the slips before every draw, the nth digit of the
sequence has an equal or uniform chance of being any of the digits 0, 1,
2,3,4,5,6,7,8, 9 irrespective of all the preceding digits in the recorded
sequence.
In some simulations, we use random numbers that are between 0 and 1. For
example, if you need such numbers with four decimal digits, then you can
take four at a time from the recorded sequence of random digits, and place a
decimal point in front of each group of four.
Exercise
Suggest one or two experimental set-ups (analogous to the slip-in-a-box
approach) for generating uniform random digits.
RANDOMIZE
Suppose your response to the above prompt is 100. Then the computer
would use this number, 100, to generate the first random number. This
number generated is used to generate the next random number. Thus by
specifying the seed for the first random number, we are in a way controlling
all random numbers that will be generated until the seed is reset. A control
such as this can be very useful in validating a simulation program or other
computer programs that use random numbers. Consider the following BASIC
program:
FOR K% = 1 TO 5
PRINT RND NEXT K%
END
If the above program is run, some seven-digit decimal numbers like the
following will be displayed:
.6291626, .1948297, .6305799, .8625749, .736353. The particular
digits displayed depend on the system time.
Every time you run the above program, different sequence of numbers will
be displayed. Now add a RANDOMIZE statement to the program:
If you run this program with 300 as a response to the prompt for the random
number seed, the following may be displayed:
.1851404, .9877729, .806621, .8573399, .6208935
Exercise
Find out whether the same set of random numbers will be displayed each
time the above program is run with seed 300.
Example 1
A simple python program that will stimulate the tossing of two dice and
display the value obtained after each toss, and the total value of the dice is
shown below.
import random
def roll_dice():
# Simulate rolling two dice
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
Exercise
Run the program of example 1 several times using different random number
seeds to determine if the integers generated for the individual die are
uniformly distributed between 1 and 6 inclusive.
Example 2
Another python program to simulate the tossing of a fair coin 10 times. The
program displays a H when a head appears and a T when a tail appears.
import random
def toss_coin():
# Simulate 10 coin tosses
results = []
for _ in range(10):
toss = "H" if random.randint(0, 1) == 0 else "T"
results.append(toss)
How It Works:
random.randint(0, 1): Generates either 0 or 1 randomly.
0 is interpreted as "H" (heads).
1 is interpreted as "T" (tails).
Loop: Simulates 10 tosses.
Results Display: The results are stored in a list and printed as a space-
separated string.
Example Output:
Running the program might produce something like this:
Copy code
Coin Toss Results: H T H H T T H T T H
Example 3
Suppose the output of the program of example 3 is: HHTHHTTTHH and that
there are two players X and Y involved in the tossing of the coin. Given that
player X wins N50.00 from player Y if a head appears and loses it to player
Y if a tail appears. Determine who won the game and by how much.
Solution
From the output there are 6 heads and 4 tails.
Player X wins N50.00 x 6 = N300.00 from player Y. He loses N50.00 x 4 =
N200.00 to player Y.
Thus, player X won the game with N300.00 – N200.00 = N100.00.
Explanation of Example 3
This scenario involves a coin-toss game with two players, X and Y, and a
simple betting rule:
If a head (H) appears: Player X wins N50.00 from Player Y.
If a tail (T) appears: Player X loses N50.00 to Player Y.
Key Takeaways:
The problem is straightforward arithmetic: count occurrences of heads and
tails, calculate the corresponding monetary transactions, and compute the
net result.
The approach applies to any output of coin tosses—just count heads and
tails, and repeat the calculations based on the given rules.
ASSIGNMENT
Write a python program to generate thirty random integer numbers
distributed between 20 and 50. Your program should ensure that no number
is repeated.
2.Write a Python program using the quadratic congruential method to
generate 15 random integer numbers between 1 and 50.
def quadratic_congruential_method(seed, a, b, c, m, n):
"""
Generates random numbers using the Quadratic Congruential Method.
Args:
seed (int): Initial seed value.
a (int): Multiplier constant.
b (int): Linear constant.
c (int): Additive constant.
m (int): Modulus.
n (int): Number of random numbers to generate.
Returns:
list: List of generated random integers.
"""
random_numbers = []
x = seed # Initialize with the seed value
for _ in range(n):
x = (a * (x**2) + b * x + c) % m # Quadratic congruential formula
random_numbers.append(x if x > 0 else 1) # Ensure range starts from
1
return random_numbers
Generated Random Numbers (1 to 50): [17, 22, 29, 40, 19, 14, 43, 8, 1, 28,
50, 48, 36, 23, 42]
Verification & Validation
One of the real problems that the simulation analyst faces is to validate the
model. The simulation model is valid only if the model is an accurate
representation of the actual system, else it is invalid.
Validation and verification are the two steps in any simulation project to
validate a model.
Step 1 − Design a model with high validity. This can be achieved using the
following steps −
The model must be discussed with the system experts while designing.
The model must interact with the client throughout the process.
The output must supervised by system experts.
Step 2 − Test the model at assumptions data. This can be achieved by
applying the assumption data into the model and testing it quantitatively.
Sensitive analysis can also be performed to observe the effect of change in
the result when significant changes are made in the input data.
Step 3 − Determine the representative output of the Simulation model. This
can be achieved using the following steps −
Determine how close is the simulation output with the real system
output.
Comparison can be performed using the Turing Test. It presents the
data in the system format, which can be explained by experts only.
Statistical method can be used for compare the model output with the
real system output.
Model Data Comparison with Real Data
In this approach, we use real-world inputs of the model to compare its output
with that of the real-world inputs of the real system. This process of
validation is straightforward, however, it may present some difficulties when
carried out, such as if the output is to be compared to average length,
waiting time, idle time, etc. it can be compared using statistical tests and
hypothesis testing. Some of the statistical tests are chi-square test,
Kolmogorov-Smirnov test, Cramer-von Mises test, and the Moments test.
This method was first used by scientists working on the atom bomb in 1940.
This method can be used in those situations where we need to make an
estimate and uncertain decisions such as weather forecast predictions.