Random Number Tests
Random Number Tests
Here are the key formulas and an example from your document regarding testing random number generators:
Formula:
k
(Oi − Ei )2
χ2 = ∑
i=1
Ei
Example:
(Oi −Ei )2
Interval Oi Ei
Ei
Test Statistic:
1/4
2. Runs Test for Independence
Formula:
Test if numbers are independent based on the number of "runs" (increasing or decreasing sequences):
Mean of runs:
2n − 1
E[R] =
3
Variance of runs:
16n − 29
σR2 =
90
Test Statistic:
r − E[R]
z=
σR
Reject H0 if ∣z∣
> zα/2 (from standard normal table)
Let me know if you need a more detailed breakdown or help with another example!
Problem:
Suppose we have a sequence of 20 numbers:
0.15, 0.78, 0.22, 0.95, 0.33, 0.85, 0.11, 0.68, 0.25, 0.91, 0.42, 0.76, 0.08, 0.61, 0.37, 0.88, 0.45, 0.93, 0.12, 0.74
0.15(+)0.78(−)0.22(+)0.95(−)0.33(+)0.85(−)0.11(+)0.68(−)0.25(+)0.91(−)0.42(+)0.76(−)0.08(+)0.61(−)0.37(+)0.88(−)0.45(+
Runs: +, −, +, −, +, −, +, −, +, −, +, −, +, −, +, −, +, −, +, −
2/4
2n − 1 2(20) − 1 39
E[R] = = = = 13
3 3 3
2. Variance of Runs:
3. Standard Deviation:
4. Test Statistic:
r − E[R] 19 − 13 6
z= = = ≈ 3.33
1.8 1.8
σR
Conclusion:
At the 5% significance level, there is sufficient evidence to conclude that the sequence is not independent.
How It Works
1. Data Preparation:
Arrange the random numbers u1 , u2 , u3 , … , un into d-dimensional tuples. For example, for d
= 2, the tuples are:
Divide the d-dimensional unit hypercube [0, 1]d into k d smaller regions or cells (e.g., k = 5).
3. Count Observations in Each Cell:
Oj1 ,j2 ,…,jd : Number of d-tuples falling into the (j1 , j2 , … , jd )-th cell.
E= n
kd
: Expected number of tuples in each cell (assuming uniformity).
4. Calculate Test Statistic: The Serial Test Statistic is based on the Chi-Square formula:
k k k
(Oj1 ,j2 ,…,jd − E)2
χ2 = ∑ ∑ ⋯ ∑
E
j 1 =1 j 2 =1
j d =1
H0 : The random numbers are IID and uniformly distributed over [0, 1]d .
5. Decision Rule:
3/4
Reject H0 if χ2
> χ2α,kd −1 , where χ2α,kd −1 is the critical value for the given significance level α.
Counts:
O1 = 5, O2 = 5, O3 = 6, O4 = 4
10
4. Compute Test Statistic: E = 4
= 5 (Expected count per cell):
1 1
χ2 = 0 + 0 + + = 0.4
5 5
Conclusion:
At a 5% significance level, there is insufficient evidence to reject the hypothesis that the numbers are IID and uniformly distributed.
The Serial Test can be extended to higher dimensions d, but as d increases, the complexity and required sample size n also increase. Let me know
if you'd like to explore this further!
4/4