A Single Number Is Not Random. - Random Means The Absence of Order
A Single Number Is Not Random. - Random Means The Absence of Order
MSE485/PHY466/CSE485
1
©D.D. Johnson and D. Ceperley 2009
Random numbers on a computer
• Truly Random - the result of a physical process such as
timing clocks, circuit noise, Geiger counts, bad memory
– Too slow (we need 1010/sec) and expensive
– Low quality
– Not reproducible
• Pseudo-random. prng (pseudo means fake)
– Deterministic sequence with a repeat period but with the
appearance of randomness (if you don’t know the
algorithm).
• Quasi-random (quasi means almost random)
– “half way” between random and a uniform grid.
– Meant to fill space with max. distance between space to fill
“holes” sequentially (e.g., [0,100] as 0,1,2,3,…,100).
MSE485/PHY466/CSE485
2
©D.D. Johnson and D. Ceperley 2009
Desired Properties of RNG on a computer
• Uncorrelated Sequences:
<f1(xi+1)…fk(xi+k)>= <f1(xi+1)>…<fk(xi+k)>
Monte Carlo can be very sensitive to such
correlations, especially for large k.
MSE485/PHY466/CSE485
3
©D.D. Johnson and D. Ceperley 2009
Pseudo Random Sequence
MSE485/PHY466/CSE485
4
©D.D. Johnson and D. Ceperley 2009
Period or Cycle Length (L)
• If internal state has M bits, total state space is 2M values.
• If mapping is 1-1, space divides into a finite no. of cycles.
• Best case is a single cycle of length 2M, otherwise 2M/L.
• It is easy to achieve very long cycle length: 32 or 24 bit
generators are not long enough!
• Entire period of the RNG is exhausted in:
• rand (1 processor) ~ 100 second
• drand48 (1 processor) ~ 1 year
• drand48 (100 processor) ~ 3 days
• SPRNG LFG (105 procs) ~ 10375 years
Assuming 107 RN/sec per processor
MSE485/PHY466/CSE485
5
©D.D. Johnson and D. Ceperley 2009
Common PRNG Generators
Linear Congruential Generator (LCG) modulo (mod) is remainder after division
64-bit word (or 2 32-bit LCG) give a period of ~10 18.
• Multiplicative Lagged zn = zn-k * zn-l
Fibonacci vary
• Modified Lagged initialization
zn = zn-k + zn-l
Fibonacci
(modulo 2m )
• 48 bit LCG
• 64 bit LCG zn = a*zn-1 + p vary p
• Prime Modulus LCG (modulo m )
vary a
Recurrence Parallelization
MSE485/PHY466/CSE485
6
©D.D. Johnson and D. Ceperley 2009
Uniformity
• Output consists of taking N bits from state and making
an integer in (0,2N-1) “Ik”.
MSE485/PHY466/CSE485
7
©D.D. Johnson and D. Ceperley 2009
Example of LCG: linear congruent generator
Example of LCG(a,c,m,I0).
MSE485/PHY466/CSE485
8
©D.D. Johnson and D. Ceperley 2009
Example of LCG: In+1 = a In + c mod(m)
Thus,
LCG mod(m) Integer Binary Real
I0 : 0 + 1 = 1 - 0x16 1 0001 1/16 = 0.0625
I1: 5x1 + 1 = 6 - 0x16 6 0110 6/16 = 0.3750
I2: 5x6 + 1 = 31 - 1x16 15 1111 15/16 = 0.9375
I3: 5x15 + 1 = 76 - 4x16 12 1100 12/16 = 0.7500
I4: 5x12 + 1 = 61 - 3x16 13 1101 13/16 = 0.8125
…
I15: 5x3 + 1 = 16 - 1x16 0 0000 0/16 = 0.0000
MSE485/PHY466/CSE485
12
©D.D. Johnson and D. Ceperley 2009
SPRNG: originally a NCSA project
SPRNG Functions
• int *init_sprng(int streamnum, int nstreams, int seed, int param)
• double sprng(int *stream)
• int isprng(int *stream)
• int print_sprng(int *stream)
• int make_sprng_seed()
• int pack_sprng(int *stream, char **buffer)
• int *unpack_sprng(char *buffer)
• int free_sprng(int *stream)
• int spawn_sprng(int *stream, int nspawned, int ***newstreams)
MSE485/PHY466/CSE485
13
©D.D. Johnson and D. Ceperley 2009
What is a chisquare test
• Suppose { x1,x2,….} are N Gaussian random numbers mean zero
variance 1
N
• What is the distribution of y x 2?
k 1
2
k k
k2
k 1
MSE485/PHY466/CSE485
14
©D.D. Johnson and D. Ceperley 2009
Chi-squared test of randomness
• HW exercise: do test of several generators
– Divide up “cube” into “N” bins.
– Sample many “P” triplets.
– Number/bin should be n=P/N ±(P/N)1/2 N=100
In example: expected n = 10 ± (10)1/2
P=1000
GOOD BAD
MSE485/PHY466/CSE485
15
©D.D. Johnson and D. Ceperley 2009
See “Numerical Recipes” on reserve or on-line
(link on class webpages)
2 (Ni ni )2
• Chi-squared statistic is i
ni
– ni (Ni) is the expected (observed) number in bin i.
– Ni is an integer! (Omit terms with 0 = ni = Ni)
– Term with ni = 0 and Ni ≠ 0, correctly give χ2 = ∞.)
– Large values of χ2 indicate the “null” hypothesis, i.e. unlikely.
MSE485/PHY466/CSE485
17
©D.D. Johnson and D. Ceperley 2009
Parallel Simulations
• Problems:
– Big systems require excessive wall clock time.
– Excessive amounts of output data generated.
– Random number correlation?
MSE485/PHY466/CSE485
19
©D.D. Johnson and D. Ceperley 2009
Parallelization of RNGs
MSE485/PHY466/CSE485
20
©D.D. Johnson and D. Ceperley 2009
Examples of Parallel MC codes and Problems
MSE485/PHY466/CSE485
22
©D.D. Johnson and D. Ceperley 2009