Random Numbers: Generation and Testing
Every simulation in this course runs on random numbers: the random digits that drove the simulation tables, and the inter-arrival and service times of the single-server queue. This section shows how a computer produces those numbers with two generators, and how three statistical tests check that the numbers behave like real random numbers: uniform on [0, 1] and independent of each other. All five problems of Exercise 3 are solved step by step.
Objectives
- Compute the sequence of a multiplicative generator and find its cycle and its period.
- Compute random numbers in
[0, 1]with a linear congruential generator (LCG). - Apply the Kolmogorov-Smirnov test to check that a small sample is uniform.
- Apply the chi-square test to check that a large sample is uniform.
- Apply the autocorrelation test to check that numbers at a fixed distance are independent.
- Read the critical value from the correct table and state the decision.
1. What a random number must do
A simulation turns random numbers into random events: an arrival, a service time, a demand. For this to give correct results, the numbers must have two properties:
| Property | Meaning | Test in this section |
|---|---|---|
| Uniformity | Every part of [0, 1] gets its fair share of numbers | Kolmogorov-Smirnov, chi-square |
| Independence | A number tells nothing about the numbers after it | Autocorrelation |
The generators below compute each number from the one before it, so after some time the sequence repeats. Each test starts from a hypothesis: the numbers are uniform, or the numbers are independent. The test either rejects that hypothesis or does not reject it.
2. Generating random numbers
The multiplicative generator
- is the seed, the starting value.
- is the multiplier and is the modulus.
x mod mis the remainder after dividingxbym, so every is between0andm - 1.
Each depends only on . When a value appears for the second time, the sequence repeats from that point: the generator is cycling. The number of values in one cycle is the period. A long period is better, because the numbers start to repeat later.
Q1 (a): Z0 = 1, a = 11, m = 16
| i | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 1 | 11 | 9 | 3 | 1 | 11 |
, so the cycle is 1, 11, 9, 3 and the period is m / 4 = 16 / 4 = 4 numbers.
Q1 (b): Z0 = 2, a = 11, m = 16
| i | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 2 | 6 | 2 | 6 |
The period is 2 numbers. The values of a and m are the same as in (a); only the seed changed, and the period fell from 4 to 2. The choice of seed matters.
Q1 (c): Z0 = 1, a = 2, m = 13
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 4 | 8 | 3 | 6 | 12 | 11 | 9 | 5 | 10 | 7 | 1 |
The period is 12 = m - 1: every value from 1 to 12 appears once before the sequence repeats. This is the longest period possible for m = 13, because 0 never appears (once Z = 0, every later value is 0 too).
Note on the exercise sheet. The table on the sheet is correct, but the step lines skip one value: they go from directly to
24 mod 13 = 9. In fact24 mod 13 = 11is , and9comes one step later, as the table shows.
Q1 (d): Z0 = 2, a = 3, m = 13
| i | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 2 | 6 | 5 | 2 |
The period is only 3 numbers. With the same modulus as (c), the multiplier a = 3 gives a very short cycle, where a = 2 gave the full 12.
The linear congruential generator (LCG)
The LCG adds a constant c, the increment, before the remainder:
Every is an integer from 0 to m - 1. To get a random number in [0, 1], divide by the modulus:
Q2: X0 = 27, a = 8, c = 47
For m = 100, the remainder is the two rightmost digits:
The first three random numbers in [0, 1] are:
Note on the exercise sheet. The question gives
m = 102, but the solution computes withm = 100. We follow the solution. Withm = 102the integers are59, 9, 17, and the random numbers are59/102 = 0.578,9/102 = 0.088and17/102 = 0.167.
3. Testing uniformity
Both tests use the same hypothesis, : the numbers are uniformly distributed on [0, 1]. Both compute a statistic from the data and compare it with a critical value from a table at the level of significance . If the statistic is smaller than the critical value, is not rejected.
| Test | Statistic | Table |
|---|---|---|
| Kolmogorov-Smirnov | Largest distance | KS critical values, by N and |
| Chi-square | Chi-square critical values, by degrees of freedom and |
The Kolmogorov-Smirnov test
The test compares the sample with the ideal uniform distribution, point by point:
- Sort the numbers in ascending order: .
- Compute the largest distance above the ideal line:
- Compute the largest distance below it:
- Take .
- Read for
Nand from the KS table. If , is not rejected.
Q3: 0.54, 0.73, 0.98, 0.11, 0.68 with α = 0.05
Sorted, the numbers are 0.11, 0.54, 0.68, 0.73, 0.98, and N = 5:
| i | ||||
|---|---|---|---|---|
| 1 | 0.11 | 0.20 | 0.09 | 0.11 |
| 2 | 0.54 | 0.40 | -0.14 | 0.34 |
| 3 | 0.68 | 0.60 | -0.08 | 0.28 |
| 4 | 0.73 | 0.80 | 0.07 | 0.13 |
| 5 | 0.98 | 1.00 | 0.02 | 0.18 |
From the KS table, for N = 5. Since , is not rejected: the numbers pass the uniformity test.
The chi-square test
- Divide
[0, 1]intonequal class intervals. - Count , the number of values that fall in interval
i. - Compute the expected count for every interval: .
- Compute the statistic:
- Read from the chi-square table, with
n - 1degrees of freedom. If , is not rejected.
Q4: 100 numbers with α = 0.05
| 0.34 | 0.90 | 0.25 | 0.89 | 0.87 | 0.44 | 0.12 | 0.21 | 0.46 | 0.67 |
| 0.83 | 0.76 | 0.79 | 0.64 | 0.70 | 0.81 | 0.94 | 0.74 | 0.22 | 0.74 |
| 0.96 | 0.99 | 0.77 | 0.67 | 0.56 | 0.41 | 0.52 | 0.73 | 0.99 | 0.02 |
| 0.47 | 0.30 | 0.17 | 0.82 | 0.56 | 0.05 | 0.45 | 0.31 | 0.78 | 0.05 |
| 0.79 | 0.71 | 0.23 | 0.19 | 0.82 | 0.93 | 0.65 | 0.37 | 0.39 | 0.42 |
| 0.99 | 0.17 | 0.99 | 0.46 | 0.05 | 0.66 | 0.10 | 0.42 | 0.18 | 0.49 |
| 0.37 | 0.51 | 0.54 | 0.01 | 0.81 | 0.28 | 0.69 | 0.34 | 0.75 | 0.49 |
| 0.72 | 0.43 | 0.56 | 0.97 | 0.30 | 0.94 | 0.96 | 0.58 | 0.73 | 0.05 |
| 0.06 | 0.39 | 0.84 | 0.24 | 0.40 | 0.64 | 0.40 | 0.19 | 0.79 | 0.62 |
| 0.18 | 0.26 | 0.97 | 0.88 | 0.64 | 0.47 | 0.60 | 0.11 | 0.29 | 0.78 |
The data is classified into n = 10 intervals: [0, 0.1], (0.1, 0.2], (0.2, 0.3], and so on up to (0.9, 1.0]. With N = 100, every interval expects values.
| Interval | ||||
|---|---|---|---|---|
| 1: [0, 0.1] | 8 | 10 | -2 | 0.4 |
| 2: (0.1, 0.2] | 8 | 10 | -2 | 0.4 |
| 3: (0.2, 0.3] | 10 | 10 | 0 | 0 |
| 4: (0.3, 0.4] | 9 | 10 | -1 | 0.1 |
| 5: (0.4, 0.5] | 12 | 10 | 2 | 0.4 |
| 6: (0.5, 0.6] | 8 | 10 | -2 | 0.4 |
| 7: (0.6, 0.7] | 10 | 10 | 0 | 0 |
| 8: (0.7, 0.8] | 14 | 10 | 4 | 1.6 |
| 9: (0.8, 0.9] | 10 | 10 | 0 | 0 |
| 10: (0.9, 1.0] | 11 | 10 | 1 | 0.1 |
| Total | 100 | 100 | 0 | 3.4 |
The table value is in row n - 1 = 9 of the chi-square table, column . Since , is not rejected, and the data is considered uniformly distributed.
Note on the exercise sheet. The conclusion slide writes . The correct relation is (3.4 against 16.9), which is why is not rejected.
4. Testing independence: the autocorrelation test
Uniform numbers can still depend on each other, for example if every fifth number is always small. The autocorrelation test checks the numbers at a fixed distance:
iis the position of the first number tested.mis the lag, the distance between two tested numbers. Heremis not the modulus of a generator.Nis the number of values in the sequence.Mis the largest integer that keeps the last tested number inside the sequence:
The estimate of the autocorrelation, its standard deviation and the test statistic are:
The hypothesis : the numbers are independent. If , is not rejected. For , , so look for the cumulative probability 0.975 in the normal table: .
Q5: the 3rd, 8th, 13th, ... numbers with α = 0.05
| 0.12 | 0.01 | 0.23 | 0.28 | 0.89 | 0.31 | 0.64 | 0.28 | 0.83 | 0.93 |
| 0.99 | 0.15 | 0.33 | 0.35 | 0.91 | 0.41 | 0.60 | 0.27 | 0.75 | 0.88 |
| 0.68 | 0.49 | 0.05 | 0.43 | 0.95 | 0.58 | 0.19 | 0.36 | 0.69 | 0.87 |
The test starts at the 3rd number, so i = 3. From the 3rd to the 8th is a lag of m = 5, and N = 30.
The tested numbers are = 0.23, 0.28, 0.33, 0.27, 0.05, 0.36, shown in bold above.
Since , is not rejected: the 3rd, 8th, 13th, ... numbers look independent.
5. Common mistakes
- Forgetting to divide by
m. An LCG gives integers. The random number in[0, 1]is : in Q2,63becomes0.63. - Testing the numbers unsorted. The KS test needs the numbers in ascending order first. With the original order of Q3, the column
i/Nwould be matched with the wrong values. - Mixing up the two KS columns. uses ; uses . Each one is a maximum over its own column, and negative entries simply lose.
- Wrong degrees of freedom. The chi-square table is read at
n - 1degrees of freedom, wherenis the number of intervals, not the number of values:9, not99. - Reversing the decision. In all three tests, a statistic inside the critical limit means is not rejected. A large , a large or a large is what rejects.
- Rounding
Mup.Mis the largest integer with . In Q5,M ≤ 4.4givesM = 4;M = 5would need a 33rd number.
6. Practice
Solve each problem, then check the answers at the end of this page. Use for N = 5, , and .
- A multiplicative generator has
Z0 = 3,a = 5andm = 16. Compute the values until the sequence cycles, and give the period. - An LCG has
X0 = 7,a = 21,c = 13andm = 100. Compute the first three random numbers in[0, 1]. - Use the Kolmogorov-Smirnov test with to check the uniformity of
0.44, 0.81, 0.14, 0.05, 0.93. - Fifty numbers were classified into 5 equal intervals, with observed counts
12, 7, 9, 13, 9. Use the chi-square test with . - With the 30 numbers of Q5, test the 1st, 7th, 13th, ... numbers for autocorrelation with .
Key takeaways
- A multiplicative generator computes ; the seed, the multiplier and the modulus decide how long the period is.
- An LCG adds an increment, , and gives a number in
[0, 1]. - Kolmogorov-Smirnov: sort, compute and , take the larger, compare with .
- Chi-square: count each interval, sum , compare with the table at
n - 1degrees of freedom. - Autocorrelation: find
M, compute , and , and compare with . - In every test, a statistic inside the limit means the hypothesis is not rejected.
Answers
Practice 1
, , , , which is again. The cycle is 3, 15, 11, 7, and the period is 4 = m / 4.
Practice 2
, , . The random numbers are 0.60, 0.73 and 0.46.
Practice 3
| i | ||||
|---|---|---|---|---|
| 1 | 0.05 | 0.20 | 0.15 | 0.05 |
| 2 | 0.14 | 0.40 | 0.26 | -0.06 |
| 3 | 0.44 | 0.60 | 0.16 | 0.04 |
| 4 | 0.81 | 0.80 | -0.01 | 0.21 |
| 5 | 0.93 | 1.00 | 0.07 | 0.13 |
, , so . Since , is not rejected: the numbers are uniform.
Practice 4
. The terms are 0.4, 0.9, 0.1, 0.9, 0.1, so . With n - 1 = 4 degrees of freedom, . Since , is not rejected.
Practice 5
i = 1, m = 6, N = 30: gives , so M = 3. The tested numbers are = 0.12, 0.64, 0.33, 0.75, 0.95.
Since , is not rejected: these numbers look independent.