Random Number Generation
Random Number Generation
Random Number Generation
Generation
Outline
Properties of Random Numbers
Generation of Pseudo-Random Numbers
(PRN)
Techniques for Generating Random
Numbers
Tests for Random Numbers
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: pdf for random
1, 0 x 1 numbers
f ( x)
0, otherwise
1
2
1 x 1
E ( R ) xdx
0 2
0
2
3 1 2
x 1 1 1 1
V ( R ) x dx E R
1 2
2
0 3 0 2 3 4 12 3
Properties of Random Numbers
5
Important considerations in RN routines
7
Congruential Property
• Number theory is the systematic study of the
natural numbers.
• Divisible means that there is no reminder after
division. For example, 6 ÷ 3 = 2.
• The reminder when a number x is divided by n has
n possibilities, 0, 1, 2, …, n-1.
• If 11 is divided by 5, the reminder is 1.
• If x is divided by 5, the reminders could be 0, 1, 2,
3, or is 4.
• 11 ≡ 1 (mod 5)
• This is stated as 11 is congruent to 1 (modulo
5).
• i.e., when 11 is divided by 5, the reminder is 1.
• Xi+1 ≡ (a xi + c) mod m
The Random Number Cycle
• Almost all random number
generators have as their basis a
sequence of pseudorandom integers.
• The integers or ``fixed point''
numbers are manipulated
arithmetically to yield floating point
or ``real'' numbers.
• The Nature of the cycle
– the sequence has a finite number
of integers
– the sequence gets traversed in a
particular order
– the sequence repeats if the period
of the generator is exceeded
– the integers need not be distinct;
that is, they may repeat.
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:
X i 1 (aX i c) mod m, i 0,1,2,...
Xi
Ri , i 1,2,...
m
12
• EXAMPLE 1: Use X0 = 27, a = 17, c = 43, and m = 100.
• The Xi and Ri values are:
X1 = (17*27+43) mod 100 = 502 mod 100 = 2, R1 = 0.02;
X2 = (17*2+43) mod 100 = 77 mod 100 =77, R2 = 0.77;
X3 = (17*77+43) mod 100 = 1352 mod 100 = 52, R3 = 0.52;
…
• 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].
13
• EXAMPLE 2: LCG (5, 1, 16, 1)
– Let us consider a simple example with a = 5, c = 1, m
= 16, and X0 =1.
– The sequence of pseudorandom integers generated by
this algorithm is:
1,6,15,12,13,2,11,8,9,14,7,4,5,10,3,0,1,6,15,12,13,2,11,8,
9,14, ..
• 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.
15
Maximum Period or Cycle Length:
•For m a power of 2, say m=2b, and c 0, the longest
possible period is P=m=2b, which is achieved when c is
relatively prime to m (greatest common divisor of c and
m is 1) and a=1+4 k, where k is an integer.
•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,….
•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
Example - 3
Using the multiplicative congruential method,
find the period of the generator for a = 13, m =
26, and X0 = 1, 2, 3, and 4.
The solution is given in next slide.
When the seed is 1 and 3, the sequence has
period 16.
When the seed is 2, the sequence has period 8.
When the seed is 4, the sequence has period 4.
• Period Determination Using Various seeds
• i Xi Xi Xi Xi
• 0 1 2 3 4
• 1 13 26 39 52
• 2 41 18 59 36
• 3 21 42 63 20
• 4 17 34 51 4
• 5 29 58 23
• 6 57 50 43
• 7 37 10 47
• 8 33 2 35
• 9 45 7
• 10 9 27
• 11 53 31
• 12 49 19
• 13 61 55
• 14 25 11
• 15 5 15
• 16 1 3
Linear Congruential Generators
• Some famous values for a and m (assuming c = 0)
a = 23, m = 108 + 1 (first LCG; original 1951 implementation )
a = 65539, m = 231 –1 (RANDU generator of 1960s; poor
because of correlated output)
a = 16807, m = 231–1 (has been discussed as minimum standard
for RNGs; used in Matlab version 4)
32
Goodness-of-fit test
1 1
n 5
Let X be a random variable that is
uniformly distributed in [0,1]. The x1 x2 x3 x4 x5
cumulative distribution function (CDF) is
the diagonal from (0,0) to (1,1).
Suppose x(1), x(2),…, x(n) are samples of X.
x1, x2,…, xn be the ordered x(i)’s. x1 x2 x3 .... xn
The Empirical distribution is
number of xi ' s such that xi x
Fn ( x)
n
33
1 1
n 5
x1 x2 x3 x4 x5
Bad fit
Good fit
34
Kolmogorov-Smirnov Test
• 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
1 i N N N
1i N
35
Kolmogorov-Smirnov Test [Frequency Test]
37
• 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 i (M 1 )m N
M is the largest integer such that
• Hypothesis:
H 0 : im 0, if numbers are independent
H1 : im 0, if numbers are dependent
38
ˆ im
• Test statistic: Z0
ˆ ˆim
40
• Example 5: Test whether the 3rd, 8th, 13th, and so on, for the
output on Slide 30 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)5 30.