0% found this document useful (0 votes)
10 views4 pages

Random Number Tests

Uploaded by

aldo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Random Number Tests

Uploaded by

aldo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Random Number Tests

Here are the key formulas and an example from your document regarding testing random number generators:

1. Chi-Square (χ²) Test for Uniformity

Formula:

To test if numbers u1 , u2 , … , un are uniformly distributed:


​ ​ ​

1. Divide the interval (0, 1) into k equal segments.

2. Oi : Observed count of numbers in segment i


3. Ei = n/k : Expected count in each segment (assuming uniformity)


The Test Statistic (TS) is:

k
(Oi − Ei )2
χ2 = ∑
​ ​

​ ​

i=1
Ei ​

H0 : Numbers are uniformly distributed


Ha : Numbers are not uniformly distributed


Reject H0 if χ2 ​ > χ2α,k−1 (from critical table)


Example:

100 random numbers generated.

Divide the interval (0, 1) into k = 10 equal segments.


Compute Oi , Ei ​ ​ = 10, and the test statistic:

(Oi −Ei )2
Interval Oi Ei
​ ​

Ei

​ ​

[0.0, 0.1) 12 10 0.4

[0.1, 0.2) 9 10 0.1

[0.2, 0.3) 10 10 0.0

[0.3, 0.4) 13 10 0.9

[0.4, 0.5) 12 10 0.4

[0.5, 0.6) 8 10 0.4

[0.6, 0.7) 16 10 3.6

[0.7, 0.8) 5 10 2.5

[0.8, 0.9) 5 10 2.5

[0.9, 1.0] 10 10 0.0

Test Statistic:

χ2 = 0.4 + 0.1 + 0 + 0.9 + 0.4 + 0.4 + 3.6 + 2.5 + 2.5 + 0 = 10.8

Critical Value for α = 0.05 and k − 1 = 9 is χ29,0.05 = 16.9.


Since 10.8 < 16.9, do not reject H0 . ​

Conclusion: At the 5% significance level, the numbers appear uniformly distributed.

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!

Here is an example for the Runs Test for Independence:

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

Step 1: Determine Runs


A "run" is a sequence of consecutive increasing or decreasing numbers.

Compare each number with the next one:

If ui ​ < ui+1 , it is an increasing trend.


If ui ​ > ui+1 , it is a decreasing trend.


Mark increasing trends as + and decreasing trends as −:

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: +, −, +, −, +, −, +, −, +, −, +, −, +, −, +, −, +, −, +, −

Number of Runs (r): 19

Step 2: Compute Test Statistic


Given n = 20, use the formulas for the expected number of runs E[R] and the variance σR2 : ​

1. Expected Number of Runs:

2/4
2n − 1 2(20) − 1 39
E[R] = = = = 13
3 3 3
​ ​

2. Variance of Runs:

16n − 29 16(20) − 29 320 − 29 291


σR2 = = = = ≈ 3.23
90 90 90 90
​ ​ ​ ​ ​

3. Standard Deviation:

σR = ​ σR2 = ​ ​ 3.23 ≈ 1.8 ​

4. Test Statistic:

r − E[R] 19 − 13 6
z= = = ≈ 3.33
1.8 1.8
​ ​ ​

σR ​

Step 3: Decision Rule


At significance level α = 0.05, the critical value for z from the standard normal table is z0.025 = ±1.96. ​

Since z = 3.33 > 1.96, reject H0 . ​

Conclusion:
At the 5% significance level, there is sufficient evidence to conclude that the sequence is not independent.

Serial Test Overview


The Serial Test is used to evaluate independence and uniformity of pseudo-random numbers in higher dimensions. It extends the Chi-Square (
χ2 ) test by examining d-tuples of random numbers instead of testing individual numbers.

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:

(u1 , u2 ), (u3 , u4 ), (u5 , u6 ), …


​ ​ ​ ​ ​ ​

For d = 3, tuples would be (u1 , u2 , u3 ), (u4 , u5 , u6 ), ….


​ ​ ​ ​ ​ ​

2. Partition the d-Dimensional Space:

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 .

Ha : The random numbers are not IID or uniformly distributed.


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 α.
​ ​

Example: Serial Test for d =2


Problem:

A sequence of 20 random numbers:


0.12, 0.34, 0.56, 0.78, 0.23, 0.67, 0.89, 0.45, 0.10, 0.90, 0.13, 0.38, 0.59, 0.76, 0.27, 0.65, 0.88, 0.41, 0.15, 0.80.
1. Form Tuples: For d = 2:

(0.12, 0.34), (0.56, 0.78), (0.23, 0.67), …


2. Divide the Space: Suppose we divide the [0, 1]2 space into k = 2 intervals:
[0, 0.5) and [0.5, 1]. This creates k 2 = 4 cells:
Cell 1: [0, 0.5) × [0, 0.5)

Cell 2: [0, 0.5) × [0.5, 1]

Cell 3: [0.5, 1] × [0, 0.5)

Cell 4: [0.5, 1] × [0.5, 1]

3. Count Observations: Assign each tuple to a cell:

(0.12, 0.34): Cell 1


(0.56, 0.78): Cell 4
(0.23, 0.67): Cell 2
Continue for all tuples.

Counts:

O1 = 5, O2 = 5, O3 = 6, O4 = 4
​ ​ ​ ​

10
4. Compute Test Statistic: E = 4

= 5 (Expected count per cell):

(5 − 5)2 (5 − 5)2 (6 − 5)2 (4 − 5)2


χ2 = + + +
5 5 5 5
​ ​ ​ ​

1 1
χ2 = 0 + 0 + + = 0.4
5 5
​ ​

5. Decision: Critical value χ20.05,3


= 7.81. ​

Since 0.4 < 7.81, do not reject H0 . ​

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

You might also like