Random Number Generation: Dr. John Mellor-Crummey
Random Number Generation: Dr. John Mellor-Crummey
Random Number Generation: Dr. John Mellor-Crummey
Motivation
Desired properties of a good generator
Linear congruential generators
multiplicative and mixed
Tausworthe generators
Combined generators
Seed selection
Myths about random number generation
Whats used today: MATLAB, R, Linux
!Example:
For x0= 5, first 32 numbers are 10, 3, 0, 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, 7, 4, 5
!xs are integers in [0,16]
dividing by 16, get random numbers in interval [0,1]
cycle length
tail
period
Efficiently computable
Period should be large
dont want random numbers in a simulation to recycle
Linear-Congruential Generators
x n = a n mod m;
modulus
a,b,m > 0
Properties of LCGs
Choice of a, b, m affects
period
autocorrelation
Multiplicative LCGs
10
x n = a mod 2
11
x n = a mod m, m " 2
Consider
x n = 3x n"1 mod 31 (lcg_mprime_good)
Observations
unlike mixed LCG, xn can never be 0 when m is prime
12
bit 1: always 1
bit 2: always 0
bit 3: cycle (10) of length 2
bit 4: cycle (0110) of length 4
In general:
kth bit follows cycle
of length 2k-2, k 2
Typical of multiplicative
LCG with modulus 2k
13
In general:
kth bit follows cycle of length 2k
Typical of mixed LCG with
modulus 2k
14
LCG Cautions
15
Tausworthe Generators
n = 7,8,9,...
17
x7 + x3 +1
bn +7 " bn +3 " bn = 0, n = 0,1,2,...
bn +7 = bn +3 " bn , n = 0,1,2,...
bn = bn#4 " bn#7 ,
bn
bn-1
bn-2
n = 7,8,9,...
bn-3
bn-4
bn-5
bn-6
bn-7
out
18
!
s relatively
prime to 2q - 1: guarantees full period for xn
Advantage
xn can be generated very efficiently with wide-word shift and
exclusive or operations
Requires
storing an array of seed numbers
careful initialization of seed array
19
Fibonacci sequence:
Fibonacci RNG:
Properties
x n = x n"1 + x n -2
x n = (x n"1 + x n -2 )mod m
generate
x = B[j] + B[k]
B[j] = x
j = j -1 mod 17; k = k -1 mod 17
return x
Properties
passes most statistical tests
period = 2k(217-1) (much longer than LCGs)
20
why do this?
can increase period and randomness if two generators have different periods
Shuffle
use sequence a to pick which recent element in sequence b to return
Marsaglia & Bray (1964)
Cases
one stream needed
if RNG has full period, then any seed as good as another
22
Dont use 0
multiplicative LCGs and Tausworthe generators would stick at 0
23
24
Myths I
Myths II
26
rand function
lagged Fibonacci generator
seed
cache of 32 floating point numbers
combined with a shift register random integer generator
core: j ^= (j<<13); j ^= (j>>17); j ^= (j<<5)
properties:
period: > 21492
fairly sure all FP numbers in [e/2,1-e/2] are generated
e = 2-52
27
seed: the set of the 100 last numbers + cyclic shift of buffer
period: about 2^129.
Knuth-TAOCP-2002
initialization of GFSR from seed was altered
28
Wichmann-Hill
seed: integer vector of length 3
seed[i] is in 1:(p[i] - 1)
p is the length 3 vector of primes, p = (30269, 30307, 30323)
Super-Duper (Marsaglia)
doesnt pass the MTUPLE test of the Diehard battery
period: about 4.6*10^18 for most initial seeds
seed: 2 integers (first: all values allowed; second: odd value).
default seeds are the Tausworthe and congruence long integers
29
random function
non-linear additive feedback-based generator
state: 8, 32, 64, 128, or 256 bytes
all bits considered random
rand function
bottom 12 bits go through cyclic pattern
higher-order bits more random
30