Random Number Generation
Random Number Generation
Outline
1. Properties of Random Numbers
2. Generation of Pseudo-Random Numbers (PRN)
3. Techniques for Generating Random Numbers
4. Tests for Random Numbers
1. Properties of Random Numbers
A sequence of random numbers R1, R2, …, must have two important statistical properties:
• Uniformity
• Independence.
Random Number, Ri, must be independently drawn from a uniform distribution with pdf:
4
2. Generation of Pseudo-Random Numbers (PRN)
5
2. Generation of Pseudo-Random Numbers (PRN)
Problems that occur in generation of pseudo-random numbers
(PRN)
• Generated numbers might not be uniformly distributed
• Generated numbers might be discrete-valued instead of continuous-
valued
• Mean of the generated numbers might be too low or too high
• Variance of the generated numbers might be too low or too high
• There might be dependence (i.e., correlation)
6
2. Generation of Pseudo-Random Numbers (PRN)
7
2. Generation of Pseudo-Random Numbers (PRN)
8
3. Techniques for Generating Random Numbers
3.1 Linear Congruential Method (LCM).
• Most widely used technique for generating random numbers
3.2 Combined Linear Congruential Generators (CLCG).
• Extension to yield longer period (or cycle)
3.3 Random-Number Streams.
9
3. Techniques for Generating Random Numbers: Linear
Congruential Method
To produce a sequence of integers, X1, X2, … between 0 and m-1 by
following a recursive relationship:
10
3. Techniques for Generating Random Numbers: Linear
Congruential Method
Xi
Ri , i 1,2,...
m
11
3. Techniques for Generating Random Numbers: Linear
Congruential Method
Notice that the numbers generated assume values only from the set I =
{0,1/m,2/m,….., (m-1)/m} because each Xi is an integer in the set {0,1,2,
….,m-1}
Thus each Ri is discrete on I, instead of continuous on interval [0,1]
12
3. Techniques for Generating Random Numbers: Linear
Congruential Method
Maximum Density
• Such that the values assumed by Ri, i = 1,2,…, leave no large gaps on
[0,1]
• Problem: Instead of continuous, each Ri is discrete
• Solution: a very large integer for modulus m (e.g., 231-1, 248)
Maximum Period
• To achieve maximum density and avoid cycling.
• Achieved by: proper choice of a, c, m, and X0.
Most digital computers use a binary representation of numbers
• Speed and efficiency are aided by a modulus, m, to be (or close to) a
power of 2.
13
3. Techniques for Generating Random Numbers: Linear
Congruential Method
14
3. Techniques for Generating Random Numbers : Linear
Congruential Method
Example: Using the multiplicative congruential method, find the period of the generator for a=13,
m=26=64 and X0=1,2,3 and 4
i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Xi 1 13 41 21 17 29 57 37 33 45 9 53 49 61 25 5 1
Xi 2 26 18 42 34 58 50 10 2
Xi 3 39 59 63 51 23 43 47 35 7 27 31 19 55 11 15 3
Xi 4 52 36 20 4
m=64, c=0; Maximal period P=m/4 = 16 is achieved by using odd seeds X0=1 and X0=3 (a=13 is of
the form 5+8k with k=1)
With X0=1, the generated sequence {1,5,9,13,…,53,57,61} has large gaps
Not a viable generator !! Density insufficient, period too short
15
3. Techniques for Generating Random Numbers (1): Linear
Congruential Method
Example: Speed and efficiency in using the generator on a digital
computer is also a factor
Speed and efficiency are aided by using a modulus m either a
power of 2 (=2b)or close to it
After the ordinary arithmetic yields a value of aXi+c, Xi+1 can be
obtained by dropping the leftmost binary digits and then using
only the b rightmost digits
16
3. Techniques for Generating Random Numbers : Linear
Congruential Method
Example: c=0; a=75=16807; m=231-1=2,147,483,647 (prime #)
Period P=m-1 (well over 2 billion)
Assume X0=123,457
X1=75(123457)mod(231-1)=2,074,941,799
R1=X1/231=0.9662
X2=75(2,074,941,799) mod(231-1)=559,872,160
R2=X2/231=0.2607
X3=75(559,872,160) mod(231-1)=1,645,535,613
R3=X3/231=0.7662
……….
Note that the routine divides by m+1 instead of m. Effect is negligible for such large
values of m.
17
3. Techniques for Generating Random Numbers: Combined Linear
Congruential Generators.
18
3. Techniques for Generating Random Numbers: Combined Linear
Congruential Generators
L’Ecuyer suggests how this can be done:
• If Wi,1, Wi,2,….,Wi,k are any independent, discrete valued
random variables (not necessarily identically distributed)
• If one of them, say Wi,1 is uniformly distributed on the
integers from 0 to m1-2, then
k
Wi Wi , j mod m1 1
j 1
is uniformly distributed on the integers from 0 to m1-2
19
3. Techniques for Generating Random Numbers: Combined Linear
Congruential Generators
Let Xi,1, Xi,2, …, Xi,k, be the ith output from k different multiplicative
congruential generators.
• The jth generator:
– Has prime modulus mj and multiplier aj and period
is mj-1
– Produced integers Xi,j is approx ~ Uniform on
integers in [1, mj-1]
– Wi,j = Xi,j -1 is approx ~ Uniform on integers in [0,
mj-2]
20
3. Techniques for Generating Random Numbers: Combined Linear
Congruential Generators
Suggested form:
Xi
m , Xi 0
k
X i (1) X i , j mod m1 1
j 1
Hence, Ri 1
j 1 m 1
1 , Xi 0
m1
S X
i b (bi =
• Older generators:
for i 1,2, , P b b = 1037.
1) 105; Newer generators:
23
3. Techniques for Generating Random Numbers: Random-
Numbers Streams
A single random-number generator with k streams can act like k
distinct virtual random-number generators
To compare two or more alternative systems.
• Advantageous to dedicate portions of the pseudo-random number
sequence to the same purpose in each of the simulated systems.
24
4. Tests for Random Numbers: Principles
25
4. Tests for Random Numbers: Principles
Two categories:
• Testing for uniformity. The hypotheses are:
H0: Ri ~ U[0,1]
H1: Ri ~ U[0,1] /
27
4. Tests for Random Numbers: Principles
28
4. Tests for Random Numbers: Frequency Tests
Test of uniformity
Two different methods:
• Kolmogorov-Smirnov test
• Chi-square test
Both these tests measure the degree of agreement between the distribution of a
sample of generated random numbers and the theoretical uniform distribution
Both tests are based on null hypothesis of no significant difference between
the sample distribution and the theoretical distribution
29
4. Tests for Random Numbers: Frequency Tests Kolmogorov-
Smirnov Test
Compares the continuous cdf, F(x), of the uniform distribution with the
empirical cdf, SN(x), of the N sample observations.
• We know: F ( x ) x, 0 x 1
• If the sample from the RN generator is R1, R2, …, RN, then the empirical
cdf, SN(x) is:
number of R1 , R2 ,..., Rn which are x
S N ( x)
N
The cdf of an empirical distribution is a step function with jumps at each
observed value.
30
4. Tests for Random Numbers: Frequency Tests Kolmogorov-
Smirnov Test
Test is based on the largest absolute deviation statistic between F(x) and SN(x) over the
range of the random variable:
D = max| F(x) - SN(x)|
The distribution of D is known and tabulated (A.8) as function of N
Steps:
1. Rank the data from smallest to largest. Let R(i) denote ith smallest observation,
so that R(1)R(2)…R(N)
2. Compute i i 1
D max R i ; D max R i
1i N N N
1 i N
3. Compute D= max(D+, D-)
4. Locate in Table A.8 the critical value D, for the specified significance level
and the sample size N
5. If the sample statistic D is greater than the critical value D, the null
hypothesis is rejected. If D D, conclude there is no difference
31
4. Tests for Random Numbers: Frequency Tests Kolmogorov-
Smirnov Test
Example: Suppose 5 generated numbers are 0.44, 0.81, 0.14, 0.05, 0.93.
32
4. Tests for Random Numbers: Frequency Tests - Chi-Square
Test
Chi-square test uses the sample statistic:
i 1 Ei Oi is the observed
# in the ith class
33
4. Tests for Random Numbers : Frequency Tests - Chi-Square
Test
Example 7.7: Use Chi-square test for the data shown below with =0.05. The
test uses n=10 intervals of equal length, namely [0,0.1),[0.1,0.2), ….,
[0.9,1.0)
34
4. Tests for Random Numbers: Frequency Tests Chi-Square Test
The value of 02=3.4; The critical value from table A.6 is 0.05,92=16.9.
Therefore the null hypothesis is not rejected
35
What does autocorrelation look like?
Here is a list of random integers generated. See if you can identify
a pattern by looking at the values in the sequence.
1 2 3 4 5 6 7 8 9 10
0.63 0.28 0.30 0.42 0.97 0.05 0.71 0.63 0.17 1.0
0.61 0.19 0.94 0.64 0.84 0.54 0.56 0.57 0.09 0.99
0.01 0.10 0.69 0.38 0.93 0.85 0.68 0.14 0.18 0.84
0.19 0.71 0.44 0.72 0.95 0.28 0.96 0.51 0.50 0.89
0.66 0.31 0.50 0.33 0.89 0.54 0.73 0.76 0.62 0.92
If you look carefully at the following table of random integers
generated, you'll notice that every number in the 5th, 10th, 15th, and
20th position is a larger value.
1 2 3 4 5 6 7 8 9 10
0.63 0.28 0.30 0.42 0.97 0.05 0.71 0.63 0.17 1.0
0.61 0.19 0.94 0.64 0.84 0.54 0.56 0.57 0.09 0.99
0.01 0.10 0.69 0.38 0.93 0.85 0.68 0.14 0.18 0.84
0.19 0.71 0.44 0.72 0.95 0.28 0.96 0.51 0.50 0.89
0.66 0.31 0.50 0.33 0.89 0.54 0.73 0.76 0.62 0.92
4. Tests for Random Numbers: Tests for Autocorrelation
The test for autocorrelation are concerned with the dependence between
numbers in a sequence.
Consider:
38
4. Tests for Random Numbers: Tests for Autocorrelation
Testing the autocorrelation between every m numbers (m is a.k.a. the lag),
starting with the ith number
• The autocorrelation im between numbers: Ri, Ri+m, Ri+2m, Ri+(M+1)m
• M is the largest integer such that
i (M 1 )m N
Hypothesis:
H 0 : im 0, if numbers are independent
H1 : im 0, if numbers are dependent
39
4. Tests for Random Numbers : Tests for Autocorrelation
ˆ im
Test statistics is: Z0
ˆ ˆim
41
4. Tests for Random Numbers: Tests for Autocorrelation
Example: Test whether the 3rd, 8th, 13th, and so on, for the output on Slide 35 are
auto-correlated or not.
• Hence, = 0.05, i = 3, m = 5, N = 30, and M = 4. M is the largest
integer such that 3+(M+1)530.
• From Table A.3, z0.025 = 1.96. Hence, the hypothesis is not rejected.
42
4. Tests for Random Numbers: Tests for Autocorrelation
Shortcoming:
The test is not very sensitive for small values of M, particularly when the
numbers being tested are on the low side.
Problem when “fishing” for autocorrelation by performing numerous tests:
• If = 0.05, there is a probability of 0.05 of rejecting a true hypothesis.
• If 10 independent sequences are examined,
– The probability of finding no significant autocorrelation, by
chance alone, is 0.9510 = 0.60.
– Hence, the probability of detecting significant
autocorrelation when it does not exist = 40%
43