2WB05 Simulation Lecture 5: Random-Number Generators: Marko Boon
2WB05 Simulation Lecture 5: Random-Number Generators: Marko Boon
Lecture 5: Random-number
generators
Marko Boon
http://www.win.tue.nl/courses/2WB05
December 6, 2012
Random-number generators 2/30
It is important to be able to efficiently generate independent random variables from the uniform distribution on
(0, 1), since:
• Random variables from all other distributions can be obtained by transforming uniform random variables;
• Simulations require many random numbers.
• Cryptographically secure
Midsquare method
Start with a 4-digit number z 0 (seed)
Square it to obtain 8-digits (if necessary, append zeros to the left)
Take the middle 4 digits to obtain the next 4-digit number z 1 ; then square z 1 and take the middle 4-digits again
and so on.
We get uniform random number by placing the decimal point at the left of each z i (i.e., divide by 10000).
Midsquare method
Examples
• For z 0 = 1234 we get 0.1234, 0.5227, 0.3215, 0.3362, 0.3030, 0.1809, 0.2724, 0.4201, 0.6484, 0.0422,
0.1780, 0.1684, 0.8361, 0.8561, 0.2907, ...
• For z 0 = 2345 we get 0.2345, 0.4990, 0.9001, 0.0180, 0.0324, 0.1049, 0.1004, 0.0080, 0.0064, 0.0040,
... Two succesive zeros behind the decimal will never disappear.
• For z 0 = 2100 we get 0.2100, 0.4100, 0.8100, 0.6100, 0.2100, 0.4100, ... Already after four numbers the
sequence starts to repeat itself.
Clearly, random-number generators involve a lot more than doing ‘something strange’ to a number to obtain
the next.
u n = z n /m
• For (a, c, m) = (1, 5, 13) and z 0 = 1 we get the sequence 1, 6, 11, 3, 8, 0, 5, 10, 2, 7, 12, 4, 9, 1, ... which has
full period (of 13).
• For (a, c, m) = (2, 5, 13) and z 0 = 1 we get the sequence 1, 7, 6, 4, 0, 5, 2, 9, 10, 12, 3, 11, 1, ... which has a
period of 12. If we take z 0 = 8, we get the sequence 8, 8, 8, ... (period of 1).
z n = az n−1 mod m, n = 1, 2, . . .
They cannot have full period, but it is possible to obtain period m − 1 (so each integer 1, ..., m − 1 is obtained
exactly once in each cycle) if a and m are chosen carefully. For example, as a = 630360016 and m = 231 − 1.
u n = z n /m
Disadvantage:
Consider the case k = 2 (the Fibonacci generator). If we take three consecutive numbers u n−2 , u n−1 and u n ,
then it will never happen that
whereas for true uniform variables both of these orderings occurs with probability 1/6.
java.util.Random is a Linear Congruential Generator using a 48-bit seed. (Meaning that m = 248 , the
other parameters are chosen such that the generator has maximum period.)
Old versions of this class used System.currentTimeMillis() as default random seed. Disadvantage:
two instances created in the same millisecond generate the same sequence of pseudo-random numbers.
In more detail: lower bits will be “less random” than the upper bits. For this reason, Random.nextInt()
uses the top 32 bits of the 48-bit random seed.
bit 1 bit 32
Kolmogorov-Smirnov test
Let Fn (x) be the empirical distribution function, so
number ofX i0 s ≤ x
Fn (x) =
n
Then
Dn = sup |Fn (x) − F(x)|
x
has the Kolmogorov-Smirnov (K-S) distribution.
Now we reject H0 if
Dn > dn,1−α
where dn,1−α is the 1 − α quantile of the K-S distribution.
F(x) = x, 0 ≤ x ≤ 1.
Chi-Square test
Divide the range of F into k adjacent intervals
Let
N j = number of X i ’s in [a j−1 , a j )
and let p j be the probability of an outcome in (a j−1 , a j ], so
If H0 is true, then np j is the expected number of the n X i ’s that fall in the j-th interval, and so we expect χ 2 to
be small.
If H0 is true, then the distribution of χ 2 converges to a chi-square distribution with k − 1 degrees of freedom as
n → ∞.
The chi-square distribution with k − 1 degrees of freedom is the same as the Gamma distribution with parame-
ters (k − 1)/2 and 2.
Hence, we reject H0 if
χ 2 > χk−1,1−α
2
where χk−1,1−α
2 is the 1 − α quantile of the chi-square distribution with k − 1 degrees of freedom.
Then
k
k X n 2
χ =
2
Nj −
n k
j=1
Example:
Consider the linear congruential generator
z n = az n−1 mod m
z 0 = 1973272912
Generating n = 215 = 32768 random numbers Ui and dividing (0, 1) in k = 212 = 4096 subintervals yields
χ 2 = 4141.0
Since
χ4095,0.9 ≈ 4211.4
we do not reject H0 at level α = 0.1.
Serial test
This is a 2-dimensional version of the chi-square test to test independence between successive observations.
We generate U1 , . . . , U2n ; if the Ui ’s are really i.i.d. U (0, 1), then the nonoverlapping pairs
are i.i.d. random vectors uniformly distributed in the square (0, 1)2 .
Permutation test
Look at n successive d-tuples of outcomes
. . . , (U(n−1)d , . . . , Und−1 );
Among the d-tuples there are d! possible orderings and these orderings are equally likely.
• Determine the frequencies of the different orderings among the n d-tuples;
• Apply a chi-square test to these data.
Runs-up test
Divide the sequence U0 , U1 , . . . in blocks, where each block is a subsequence of increasing numbers followed
by a number that is smaller than its predecessor.
Example: The realization 1,3,8,6,2,0,7,9,5 can be divided in the blocks (1,3,8,6), (2,0), (7,9,5).
Correlation test
Generate U0 , U1 , . . . , Un and compute an estimate for the (serial) correlation
Pn
(Ui − Ū (n))(Ui+1 − Ū (n))
ρ̂1 = i=1 Pn
i=1 (Ui − Ū (n))
2
If the Ui ’s are really i.i.d. U (0, 1), then ρ̂1 should be close to zero. Hence we reject H0 is ρ̂1 is too large.
If you still have problems with java.util.Random you can use the code given in the Numerical recipes
(combining two XORShift generators with an LCG and a multiply with carry generator).
private long u;
private long v = 4101842887655102017L;
private long w = 1;