Unit 5_ Random Numbers
Unit 5_ Random Numbers
https://collegenote.pythonanywhere.com 1
RANDOM NUMBER
UNIT 5
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 2
Random Number
• Random numbers are characterized by the fact that their
value can not be predicted.
• Or, in other words, if one constructs a sequence of
random numbers, the probability distribution of
the following random numbers have to be
completely independent of all the other
generated numbers.
• Random numbers are samples drawn from a uniformly
distributed random variable between some satisfied
intervals, they have equal probability of
occurrence.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 3
Random Number
• Random numbers are a necessary basic ingredient
(element) in the simulation of almost all discrete systems.
• Most computer languages have a subroutine, object, or
function that will generate a random number.
• Similarly simulation languages generate random numbers
that are used to generate event limes and other random
variables.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 4
So,
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 10
EXAMPLE 3.1
Q.N. Use the linear congruential method to generate a
sequence of random numbers with X 0 = 27, a= 17, c = 43,
and m = 100. Here, the integer values generated will all be
between zero and 99 because of the value of the modulus.
These random integers should appear to be uniformly
distributed the integers zero to 99. Random numbers
between zero and 1 can be generated by
Ri =Xi/m, i= 1,2,……(3.1)
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 27
solution
The sequence of Xi and subsequent Ri values is computed as follows:
X0 = 27
X1 = (17x27 + 43) mod 100 = 502 mod 100 = 2
R1=2⁄100=0.02
X2 = (17 x 2 + 43) mod 100 = 77 mod 100 = 77
R2=77 ⁄100=0.77
X3 = (17 x 77+ 43) mod 100 = 1352 mod 100 = 52
R3=52 ⁄100=0.52
First, notice that the numbers generated from Equation (3.2) can only
assume values from the set i= {0,1 /m, 2/m,..., (m — l)/m), since each Xi is
an integer in the set {0,1,2,..., m -1}. Thus, each Ri is discrete on i, instead
of continuous on the interval [0, 1], This approximation appears to be of
little consequence, provided that the modulus m is a very large integer.
(Values such as m = 231 -1 and m = 248 are in common use in generators
appearing in many simulation languages). By maximum density is meant
that the values assumed by Ri = 1,2,..., leave no large gaps on [0,1].
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 28
EXAMPLE 3.2
Q.N >Let m = 100, a = 19, c = 0, and X0 = 63, and
generate a sequence random integers. Find first 7 random
number generate using any suitable method??
Solution
X0 = 63
X1 = (19)(63) mod 100 = 1197 mod 100 = 97
X2 = (19) (97) mod 100 = 1843 mod 100 = 43
X3 = (19) (43) mod 100 = 817 mod 100 = 17
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 29
EXAMPLE 3.3
Q.N > Let a = 75, m = 231-1 and c= 0. These choices
satisfy the conditions that insure a period of P = m-1.
Further, specify a seed, X0 = 123,457.
Solution:
The first few numbers generated are as follows:
X1= 75(123,457) mod (231 - 1) = 2,074,941,799 mod (231 - 1)
X1 = 165
X2 = 75(2,074,941,799) mod (231 - 1) = 559,872,160 =185
EXAMPLE 3.4
Q.N > Using the multiplicative congruential method, find the
period of the generator for a = 13, m = 64, and X 0 = 1, 2, 3,
and 4. Prove that the solution is given , when the seed is 1
and 3, the sequence has period 16, a period of length eight
is achieved when the seed is 2 and a period of length four
occurs when the seed is 4.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 31
solution
Period Determination Using Various seeds
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 32
EXAMPLE 3.5
Q.N > Suppose that the five numbers 0.44 , 0.81, 0.14, 0.05, 0.93 were
generated, and it is desired to perform a test for uniformity using the
Kolmogorov-Smirnov test with a level of significance a of 0.05.
Solution
First, the numbers must be ranked from smallest to largest
i.e. 0.05 , 0.14 , 0.44 , 0.81 , 0.93
Then ,
Therefore,
D = max{0.26, 0.21} = 0.26.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 42
Example 3.6
• Suppose that the five numbers 0.24 , 0.80, 0.11, 0.05,
0.93 were generated, and it is desired to perform a test for
uniformity using the Kolmogorov-Smirnov test with a level
of significance a of 0.01.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 44
Example 3.7
• Suppose that the four numbers 0.80, 0.14, 0.05, 0.5 were
generated, and it is desired to perform a test for uniformity
using the Kolmogorov-Smirnov test with a level of
significance a of 0.10.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 45
Example 3.8
• Suppose that the seven numbers 0.44 , 0.81, 0.14, 0.05,
0.93, 0.01, 0.02 were generated, and it is desired to
perform a test for uniformity using the Kolmogorov-
Smirnov test with a level of significance a of 0.05.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 46
Where,
Oi is the observed number in the i-th class,
Ei is the expected number in the i-th class, and
n is the number of classes.
For the uniform distribution, Ei the expected number in each class
is given by:
Ei = N/n
for equally spaced classes, where N is the total number of
observations. It can be shown that the sampling distribution of X02 is
approximately the chi-square distribution with n - 1 degrees of freedom
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 47
Example 3.8
Q.N > Use the chi-square test with a = 0.05 to test whether
the data shown below are uniformly distributed.
0.34 0.83 0.96 0.47 0.79 0.99 0.37 0.72 0.06 0.18 0.90
0.76 0.99 0.30 0.71 0.17 0.51 0.43 0.39 0.26 0.25 0.79
0.77 0.17 0.23 0.99 0.54 0.56 0.84 0.97 0.89 0.64 0.67
0.82 0.19 0.46 0.01 0.97 0.24 0.88 0.87 0.70 0.56 0.56
0.82 0.05 0.81 0.30 0.40 0.64 0.44 0.81 0.41 0.05 0.93
0.66 0.28 0.94 0.64 0.47 0.12 0.94 0.52 0.45 0.65 0.10
0.69 0.96 0.40 0.60 0.21 0.74 0.73 0.31 0.37 0.42 0.34
0.58 0.19 0.11 0.46 0.22 0.99 0.78 0.39 0.18 0.75 0.73 0.79
0.29 0.67 0.74 0.02 0.05 0.42 0.49, 0.49 0.05 0.62 0.78
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 50
Solution
Above Table contains the essential computations for chi square test. The
test uses n = 10 intervals of equal length, namely [0.0, 0.1), [0.1, 0.2), . . . , [0.9,
1.0). The value of X2 is 3.4.
Here degree of freedom is n-1=10-1=9 and α=0.05. The tabulated value
of X2 0.05, 9 =16.9.Since X02 is much smaller than the tabulated value of chi square,
the null hypothesis of a uniform distribution is not rejected.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 51
Example 3.9
Q.N > Use the chi-square test with a = 0.05 to test whether
the data shown below are uniformly distributed
In first ranges there are 15 random number , in
second there are 5 random number, in 3rd there are 10
random number, in fourth there are 10 and in 5 th there are
20 random number.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 53
Example 3.10
Q.N > Use the chi-square test with a = 0.99 to test whether
the data shown below are uniformly distributed
In first ranges there are 10 random number , in
second there are 10 random number, in rd3 there are 15
random number, in fourth there are 15 , in 5 th there are 5
random number. And then 6th ,7th and 8th has 10 , 5 , 10
random numbers.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 54
Example 3.11
Q.N > Use the chi-square test with a = 0.25 to test whether
the data shown below are uniformly distributed
In first ranges there are 20 random number , in
second there are 9 random number, in 3rd there are 15
random number, in fourth there are 15 and in 5 th there are
13 random number.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 55
Example 3.12
Q.N > Use the chi-square test with a = 0.75 to test whether
the data shown below are uniformly distributed
In first ranges there are 25 random number , in
second there are 15 random number, in rd3 there are 10
random number, in fourth there are 17 and in 5 th there are
13 random number.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 56
Exact Approximate
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 57
And
σ35= √ (13(4) + 7) / 12(4 + 1) = 0.1280
Then, the test statistic assumes the value
Z0 = -0.1945/0.1280 = -1.516
Now, the critical value is
Z0.025 = 1.96 (Zα/2 is taken in this test)
Therefore, the hypothesis of independence cannot be rejected on the
basis of this test.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 63
0.12 0.01 0.23 0.28 0.89 0.31 0.64 0.28 0.83 0.93
0.99 0.15 0.33 0.35 0.91 0.41 0.60 0.27 0.75 0.88
0.68 0.49 0.05 0.43 0.95 0.58 0.19 0.36 0.69 0.87
Then, ρ35 = 1/( 4 + 1)[ (0.23)(0.28) + (0.28)(0.33) + (0.33)(0.27) +
(0.27)(0.05) + (0.05)(0.36) ] – 0.25 = -0.1945
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 64
Gap test
The gap test is used to determine the significance of the
interval between the recurrences of the same digit. A gap of
length x occurs between the recurrences of some specified digit.
The following example illustrates the length of gaps associated
with the digit 3:
4, 1, 3, 5, 1, 7, 2, 8, 2, 0, 7, 9, 1, 3, 5, 2, 7, 9, 4, 1, 6, 3 , 3, 9, 6,
3, 4, 8, 2, 3, 1, 9, 4, 4, 6, 8, 4, 1, 3.
There are 7 three’s are there. Thus only six gaps can
occur. The first gap is of length 10 and second gap of length 7
and third gap of length zero. And so on. Similarly the gap
associated with other digits can be calculated. The theoretical
probability of first gap (of length 10 for digit 3) can be calculated
as
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 69
Gap test
The probability of a particular gap length can be
determined by a Bernoulli trail.
Gap test
1. Specify the CDF (Cumulative Distributive frequency) from
theoretical frequency distribution given by,
𝐹(𝑥) = 1 − 0.9𝑥+1
Based on the selected class interval
2. Arrange the observed sample of gaps in cumulative
distribution with the same class
3. Find 𝐷, the maximum deviation between 𝐹(𝑥) and 𝑆𝑁(𝑥) as
equation,
𝐷= | 𝐹(𝑥) − 𝑆𝑁(𝑥) |
Where SN (x) = 𝑁𝑜.𝑜𝑓 𝑔𝑎𝑝𝑠 ≤ x
𝑡𝑜𝑡𝑎𝑙 𝑛𝑜.𝑜𝑓 𝑔𝑎𝑝𝑠
Example 3.11
Q.N->Based on the frequency with which gaps occur,
analyze the 110 digits below to test whether they are
independent. Use α = 0.05.
4, 1, 3, 5, 1, 7, 2, 8, 2, 0, 7, 9, 1, 3, 5, 2, 7, 9 4, 1, 6, 3, 3, 9,
6, 3, 4, 8, 2, 3, 1, 9, 4, 4, 6, 8, 4, 1, 3, 8, 9, 5, 5, 7, 3, 9, 5, 9,
8, 5, 3, 2, 2, 3, 7, 4, 7, 0, 3, 6, 3, 5, 9, 9, 5, 5 5, 0, 4, 6, 8, 0,
4, 7, 0, 3, 3, 0, 9, 5, 7, 9, 5, 1, 6, 6, 3, 8, 8, 8, 9, 2, 9, 1, 8, 5,
4, 4, 5, 0, 2, 3, 9, 7, 1, 2, 0, 3, 6, 3
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 72
Solution
The number of gaps is given by the number of data values
minus the number of distinct digits, or 110 —10 = 100 in the
example. The numbers of gaps associated with the various
digits are as follows:
Digit 0 1 2 3 4 5 6 7 8 9
# of Gaps 7 8 8 17 10 13 7 8 9 13
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 73
Since
D = max |F(x) - SN(x) | = 0.0224
Poker Test
The Poker Test is the test for independence based on
the frequency with which certain digits are repeated with
in a series of numbers.
This test not only tests for the randomness of the
sequence of numbers, but also the digits comprising of
each of the numbers.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 82
Poker Test
The expected value of each of the combination of digits
in a number is compared with the observed value by
means of the chi-square test for independence.
The acceptance is done if the observed value of chi-
square sums for all the possible combinations of digits is
less than the acceptable value for the given degree of
freedom at the specified confidence interval.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 83
Poker Test
This test gets its name from a game of cards called
poker
This test not only tests the randomness of the sequence
of numbers, but also the digits comprising of each
number
Every random number of five digits or every sequence
of five digits is treated as poker hand.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 84
Poker Test
71549 are five different digits
55137 would be pair
33669 would be two pairs
55513 would be three of a kind
44477 would be a full house
77774 would be four of a kind
88888 would be five of a kind
• The occurrence of five of a kind is rare.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 85
Poker Test
• In 10,000 random and independent numbers of five
digits each, you may be expect the following distribution
of various combinations.
Poker Test
Poker Test - based on the frequency with which certain
digits are repeated.
Example:
0.255 0.577 0.331 0.414 0.828 0.909 0.303
0.001...
Note: a pair of like digits appear in each number
generated.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 87
Poker Test
Frequency with certain digits are repeated in a series
of numbers
Example
Poker Test
• P(three different digits)
P(second different from first) P(third different from first and second)
(0.9)(0.8) 0.72
Poker test:
Poker Test
In 3-digit numbers, there are only 3 possibilities.
P(3 different digits) =
= P(2nd diff. from 1st) * P(3rd diff. from 1st & 2nd)
= (0.9) (0.8) = 0.72
Example 3.18
Q.N.> A sequence of 1000 three-digit numbers has been
generated and an analysis indicates that 680 have three
different digits, 289 contain exactly one pair of like digits,
and 31 contain three like digits. Based on the poker test,
are these numbers independent ? Let α = 0.05. Test these
numbers using poker test for three digits.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 91
Solution
Combination, i Observed Expected (Oi-Ei)
Frequency, Frequency (Oi-Ei)2 Ei /
(Oi) (Ei)
Three Different digit 680 0.72X1000=720 -40 2.22
The appropriate degrees of freedom are one less than the number of class
intervals. Since
47.65 > X2 0.05,2 = 5.99 (tabulated value), the independence of the numbers is
rejected on the basis of this test. Here 2 or n-1 is the degree of freedom since
there are only 3 (n) classes.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 92
Example 3.19
Q.N.> A sequence of three-digit numbers has been
generated and an analysis indicates that 380 have three
different digits, 389 contain exactly one pair of like digits,
and 231 contain three like digits. Based on the poker test,
are these numbers independent ? Let α = 0.05. Test these
numbers using poker test for three digits.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 93
Example 3.20
Q.N.> A sequence of three-digit numbers has been
generated and an analysis indicates that 320 have three
different digits, 420 contain exactly one pair of like digits,
and 160 contain three like digits. Based on the poker test,
are these numbers independent ? Let α = 0.05. Test these
numbers using poker test for three digits.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 94
Example 3.21
Q.N.> A sequence of three-digit numbers has been
generated and an analysis indicates that 300 have three
different digits, 500 contain exactly one pair of like digits,
and 200 contain three like digits. Based on the poker test,
are these numbers independent ? Let α = 0.05. Test these
numbers using poker test for three digits.
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 95
solution
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 98
•
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 100
Q.N.3 Suppose that the five numbers 0.4 , 0.8, 0.34, 0.06, 0.3 were
generated, and it is desired to perform a test for uniformity using the
Kolmogorov-Smirnov test with a level of significance a of 0.05. (from
Table A.8 for a = 0.05 and N= 5, is 0.565.)
Q.N.4 Use the chi-square test with a = 0.99 to test whether the data
shown below are uniformly distributed
In first ranges there are 30 random number , in second there
are 9 random number, in 3 rd there are 25 random number, in fourth
there are 10 and in 5 th there are 15 random number. (From table
X20.99,4= 0.297)
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 104
300
400
150
35
15
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 106
Q.N.7> Based on the frequency with which gaps occur, analyze the digits below
to test whether they are independent. Use α = 0.05.
a) Exponential Distribution
The random Variate generation process is summarized in
following steps:
Equation (1) is called random Variate generator for the exponential distribution.
In general equation (1) is written as X = F -1(R)
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 113
Solution:
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 114
b) Uniform Distribution:
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 120
c) Triangular Distribution
Simulation and Modeling / [Chapter 5] Nipun Thapa
https://collegenote.pythonanywhere.com 124