Lecture 6 A
Lecture 6 A
Random-number generators
/k 1/20
12
Most random-number generators are of the form:
/k 2/20
12
A ‘good’ random-number generator should satisfy the following properties:
• Uniformity: The numbers generated appear to be distributed uniformly
on (0, 1);
• Independence: The numbers generated show no correlation with each
other;
• Replication: The numbers should be replicable (e.g., for debugging or
comparison of different systems).
• Cycle length: It should take long before numbers start te repeat;
• Speed: The generator should be fast;
• Memory usage: The generator should not require a lot of storage.
/k 3/20
12
Linear (or mixed) congruential generators
un = zn/m
A good choice of a, c and m is very important.
/k 4/20
12
A linear congruential generator has full period (cycle length is m) if and only
if the following conditions hold:
• The only positive integer that exactly divides both m and c is 1;
• If q is a prime number that divides m, then q divides a − 1;
• If 4 divides m, then 4 divides a − 1.
/k 5/20
12
Multiplicative congruential generators
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.
/k 6/20
12
Additive congruential generators
un = zn/m
These generators can have a long period upto mk .
Disadvantage:
Consider the case k = 2 (the Fibonacci generator). If we take three consecu-
tive numbers un−2 , un−1 and un , then it will never happen that
/k 8/20
12
Testing random number generators
/
k 9/20
12
Uniformity or goodness-of-fit tests:
H0: The Xi’s are i.i.d. random variables with distribution function F .
/k 10/20
12
Kolmogorov-Smirnov test
/k 11/20
12
For n ≥ 100, √
dn,0.95 ≈ 1.3581/ n
In case of the uniform distribution we have
F (x) = x, 0 ≤ x ≤ 1.
/k 12/20
12
Chi-Square test
pj = F (aj ) − F (aj−1)
Then the test statistic is
k
2
X (Nj − npj )2
χ =
j=1
npj
If H0 is true, then npj is the expected number of the n Xi ’s that fall in the
j -th interval, and so we expect χ2 to be small.
/
k 13/20
12
If H0 is true, then the distribution of χ2 converges to a chi-square distribu-
tion with k − 1 degrees of freedom as n → ∞.
Hence, we reject H0 if
χ2 > χ2k−1,1−α
where χ2k−1,1−α is the 1 − α quantile of the chi-square distribution with k − 1
degrees of freedom.
/k 14/20
12
Chi-square test for U (0, 1) random variables
Then
k
2 k X n 2
χ = Nj −
n j=1 k
/k 15/20
12
Example:
zn = azn−1 mod m
with a = 630360016, m = 231 − 1 and seed
z0 = 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.
/k 16/20
12
Serial test
We generate U1 , . . . , U2n ; if the Ui ’s are really i.i.d. U (0, 1), then the non-
overlapping pairs
/ k 17/20
12
Permutation test
/ k 18/20
12
Runs-up test
/k 19/20
12
Correlation test
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.