Random Numbers
Random Numbers
Generations (7 Hours)
1
Contents of the unit
• Random Numbers and its properties, Pseudo Random Numbers,
Methods of generation of Random Number, Tests for Randomness-
uniformity and independence , Random Variate Generation
2
Random Numbers
• Random numbers are numbers that occur in a sequence such that
two conditions are met: (1) the values are uniformly distributed over
a defined interval or set, and (2) it is impossible to predict future
values based on past or present ones.
• Random numbers are important in statistical analysis and probability
theory.
3
Random Numbers
• Properties of Random Numbers
• A sequence of random numbers, R1, R2, R3, ... Rn must have two
important properties:
• Uniformity: They are equally probable every where
• Independence: The current value of a random variable has no relation
with the previous values
4
Random Numbers
• Properties of Random Numbers
• Some consequences of the uniformity and independence properties
• If the interval (0,1) is divided into n sub-intervals of equal length, the
expected number of observations in each interval is N/n where N is
the total number of observations. Note that N has to be sufficiently
large to show this trend.
• The probability of observing a value in a particular interval is
independent of the previous values drawn.
5
Random Numbers
• Random numbers are a necessary basic ingredient in the simulation of
almost all discrete systems.
6
Random Number Tables
• A table of numbers generated in an unpredictable, haphazard that are
uniformly distributed within certain interval are called random
number table.
7
Random Number Tables
• A random number table is also called random sample table.
• There are many physical devices or process that can be used to
generate a sequence of uniformly distributed random numbers.
8
Pseudo Random Numbers
• Pseudo means false, so false random numbers are being generated.
• The goal of any generation scheme is to produce a sequence of
numbers between 0 and 1 which simulates or imitates, the ideal
properties of uniform distribution and independence as closely as
possible.
• When generating pseudo-random numbers, certain problems or
errors can occur.
• Some examples of errors includes the following
9
Pseudo Random Numbers
1. The generated numbers may not be uniformly distributed
2. The generated numbers may be discrete-valued instead continuous
valued.
3. The mean of the generated numbers may go too high or too low
4. The variance of generated numbers may too high or low
5. There may be dependence
10
Properties of Good random Numbers
Generators
• Usually, random numbers are generated by a digital computers as part of
the simulation.
• Numerous methods can be used to generate the random numbers.
• In selecting among these methods, there are some important
considerations.
1. The routine should be fast: The total cost can be managed by selecting
a computationally efficient method of random-number generation.
2. The routine should be portable to different computers, and ideally to
different programming languages. This is desirable so that the
simulation program produces the same result whenever it is executed
11
Properties of Good random Numbers
Generators
• The routine should have a sufficiently long cycle. The cycle length, or period
represents the length of the random number sequence before previous numbers
begin to repeat themselves in an earlier order.
• The random numbers should be replicable. Given the starting point, it should be
possible to generate the same set of random numbers, completely independent
of the system that is being simulated.
• This is helpful for debugging purpose and is a means of facilitating comparisons
between systems
15
Methods to generate Random Numbers
1. For m a power of 2 say m = 2b and c , the longest possible period P
= m = 2b, which is achieved whenever c is relatively prime to m and
a = 1+4k where k is an integer.
2. For m a power of 2, say m = 2b and c =0, the longest possible
period is P = m/4 = 2b-2, which is achieved if the seed X0 is odd and
if the multiplier a is given by a = 3+8k or a = 5+8k for some
k=0,1,2..
3. For m a prime number and c = 0, the longest possible period is P =
m-1, which is achieved whenever the multiplier a has the property
that the smallest integer k such that ak-1 is divisible by m is k = m-1
16
Methods to generate Random Numbers
• Speed and efficiency in using the generator on a digital computer is
also a selection consideration.
• Speed and efficiency are aided by use of a modulo, m, which is either
a power of 2 or close to a power of 2.
• Since most digital computers use a binary representation of numbers,
the modulo or remaindering , operation of equation (1) can be
conducted efficiently when the modulo is a power of 2.
17
Example 1
• Use the linear congruential method to generate a sequence of
random numbers with X0 = 7, a = 17, c = 43 and m = 100.
• Solution:
• Here, the integer values generated will all be between zero and 99
because of the value of the modulus.
• Xi+1 = (aXi+c) mod m
• X0 = 27
• X1 =(a*X0+c) mod m = (17*27+43) mod 100 = 2
• R1 = 2/100 = 0.02
18
• X2 = (a*X1+c) mod m = (17*2+43) mod 100 = 77
• R2 = 77/100 = 0.77
• X3 = (a*X2+c) mod m = (17*77+43) mod 100 = 52
• R3 = 52/100 = 0.52
• X4 = (a*X3+c) mod m = (17*52+43) mod 100 = 27
• R4 = 27/100 = 0.27
• X5 = (a*X4+c) mod m = (17*27+43) mod 100 = 2
19
Example 2
• Using the multiplicative congruential method, find the period of the
generator for a = 13, m = 26 = 64 and X0 = 1,2,3 and 4. When the seed
is 1 or 3, the sequence has period 16. However, a period of length 8 is
achieved when the seed is 2 and a period of length 4 occurs when the
seed is 4
• Solution:
• Here m = 26 = 64 and c = 0. The maximum period of therefore P =
m/4 = 16.
• This period is achieved by using odd seeds, X0 = 1 and X0 = 3.
• Note that a = 13 is of the form 5+8k with k = 1 as is required to
achieve maximum period.
20
Example 2
• When X0 = 1, the generated sequence assumes values from the set
{1,5,9,13,…53,57,61}.
• The gaps in the sequence of generated random numbers Ri are quite
large, such as gap gives rise to concern about the density of the
generated sequence.
• The generator in example 3 is not viable for any application-its period
is too short and its density is insufficient.
• However, the example shows that importance of properly choosing a,
c , m and X0.
21
Example 3
• Let X0 = 63, a = 19, c = 0 and m = 102 = 100, and generate a sequence of
random integers using equation (1).
• Solution:
• X0 = 63
• X1 = (19*63) mod 100 = 97
• X2 = (19*97) mod 100 = 43
• X3 = (19*43) mod 100 = 17
• When m is a power of 10, say m = 10b, the modulo operation is
accomplished by saving the b rightmost digits.
• By analogy, the modulo operation is most efficient for binary computers
when m = 2b for some b>0
22
Example 4
• Let a = 75 = 16,807, c = 0, and m = 231-1 = 2,147,483,647 (a prime
number). These choices satisfy the conditions that ensure a period of
P = m-1 (over 2 billion). Further, specify the seed X0 = 123, 457. the
first few numbers generated are as follows:
• X1 = (16,807* 123, 457) mod 2,147,483,647 =
23
Random number generation using mid square
method
• This method was proposed by Van Neumann. In this method, we have a
seed and then the seed is squared and its midterm is fetched as the
random number.
• Consider we have a seed having N digits we square that number to get a
2N digits number if it doesn’t become 2N digits we add zeros before the
number to make it 2N digits.
• A good algorithm is basically the one which does not depend on the
seed and the period should also be maximally long that it should almost
touch every number in its range before it starts repeating itself as a rule
of thumb remember that longer the period more random is the
number.
24
The main problem of this method is that 0 can
Be generated in the sequence
25
Testing for Randomness
• The desirable properties of random numbers – uniformity and
independence to ensure that these desirables properties are
achieved, a number of tests can be performed.
• The tests can be placed into two categories according to the
properties of interest.
• Testing for uniformity.
• Testing for independence.
26
Testing for Randomness
• The desired properties of random numbers are uniformity and
independence.
• So, the test of random numbers means uniformity and independence
test.
• There are different types of test used for these purposes.
• They are as follows
1. Frequency Test: Uses the Kolmogorov Smirnov (KS) or Chi-square
test to compare the distribution of the set of numbers generated to
a uniform distribution.
27
Testing for Randomness
• 2 Runs Test: Tests the runs up and down or runs above or below the
mean by comparing the actual value to expected value.
• Gap Test: Counts the number of digits that appear between repetition
of a particular digit and then uses KS test to compare with the
expected size of gaps
28
Testing for Randomness
• Poker Test: Treats numbers group together as a poker hand. Then the
hands obtained are compared to what is expected using the Chi-
square test.
29
Frequency Test
• Kolmogorov Smirnov Test
• Chi-Square Test
• Both of these tests measure the degree of agreement between the
distribution of a sample of generated random numbers and the
theoretical distribution.
• Both tests are based on the null hypothesis of no-significant
difference between the sample distribution and theoretical
distribution.
30
Frequency Test
• 1. The Kolmogorov- Smirnov (KS) test: This test compares the
continuous cdf F(x) of the uniform distribution with the empirical cdf
SN(x) of the sample of N observations.
• By definition,
• F(x) = x , 0≤x ≤ 1.
31
Frequency Test
• The Kolmogorov test is based on the largest absolute deviation
between F(x) and SN(x) over the range of the random variable- that is
based on the statistic.
• D = max|(F(x)- SN(x)|
• The sampling distribution of D is known. It is obtained from table.
• The steps are a follows:
33
Frequency Test
• Step 5: If the sample statistics D is greater than the critical value Dα,
the null hypothesis that the data are a sample from a uniform
distribution is rejected.
• If D<=Dα, conclude that no difference has been detected between the
true distribution of {R1, R2,….RN} and the uniform distribution.
34
Example
• Suppose that the five numbers 0.44, 0.81.0.14,0.05, 0.93 are generated aid
it is desired to perform a test for uniformity by using the Kolmogorov –
Smirnov test with the level of significance α = 0.05.
• Solution: Given random numbers are: 0.44, 0.81.0.14,0.05, 0.93
• No of random numbers N = 5
• Step 1: Ranking random numbers from smaller to larger
• 0.05, 0.14, 0.44, 0.81. 0.93
• Step 2: Computing D+ and D- as
• D+ = max{ - Ri} for i=1,2,…N
• D- = max {Ri - } for i=1,2,,,N
• using following tables
35
i 1 2 3 4 5
Ri 0.05 0.14 0.44 0.81 0.93
i/N 0.20 0.40 0.60 0.80 1.00
i/N-Ri 0.15 0.26 0.16 --- 0.07
Ri- (i-1)/N 0.05 … 0.04 0.21 0.13
36
• Step 3: computer D = max(D+,D-)
• = max(0.26,0.21) = 0.26
• Step 4: The critical value of D from table for α = 0.05 and N = 5 is
0.565.
37
39
38
40
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 ,
39
41
https://collegenote.pythonanywhere.cSoimmulation and Modeling / [Chapter 5] Nipun Thapa
Therefore,
D = max{0.26, 0.21} = 0.26.
40
42
41
43
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.
42
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.
43
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.
44
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
X 02
approximately the be
chi-square
shown
distribution
that with the
n - 1 degrees
sampling
of freedom 45
47
46
48
47
49
Example 3.8
Q.N > Use the chi-square test with α = 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
48
49
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 X 2 is much smaller than the tabulated value of chi square,
0
50
51
51
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.
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
64
Gap test
The gap test is used to determine the significance of the
intervalbetween 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.
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 𝑆𝑁(𝑥)
equation, as
𝐷= | 𝐹(𝑥) − 𝑆𝑁(𝑥) |
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
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
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.
77
78
79
80
81
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.
83
Poker Test
This test gets its name from a game of called
cards 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.
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.
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.
87
Poker Test
Frequency with certain digits are repeated in a series
of numbers
Example
0.27
2 Given a fixed digit, this digit is the same
no. of possibilities
88
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.
91
Solution
Combination, i Observed Expected (Oi-Ei)
Frequency, Frequency (Oi-Ei)2 / Ei
(Oi) (Ei)
Three Different digit 680 0.72X1000=72 -40 2.22
0
Three Like digit 31 21 44.10
0.01X1000=1
0
Exactly one pair 289 0.27X1000=270 19 1.33
1000 1000
47.65
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.
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.
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.
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.
95
solution
98