Professional Documents
Culture Documents
RNG
2020A
Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks
Content
3 Testing randomness
5 Remarks
An experiment (physical process) is used to generate a sequence of random numbers that are
saved in computer memory as tables.
Flip a coin
Toss a die
Draw balls from an urn
Measure position of a dart from the center in a dart game
Another technique is to monitor the components of a computer to generate random numbers.
Congruential generators
A good PRNG has to yield a sequence of uniformly distributed random numbers in a range (0, 1).
Here, we will introduce commonly used generators including (a) congruential and (b) multiple
recursive.
Congruential generators
where x0 (seed), a, b, and M are given integers. IMAX is the largest integer represented by a
computer.
Congruential generators
The methods discussed here will give a random integer x in the range [0, M–1]. To convert the
x
random integer generated here into a random number on [0, 1), we use the relationship η = M
.
Let’s consider a mixed congruential generator given by
Congruential generators
The cycle in Figure 3.1 exhibits the expected behavior of a generator as follows:
The sequence has a period of 16; i.e., equivalent to the modulus, M = 24 , and the largest
number is 24 − 1.
Selection of any other number (< M = 16) as seed results in a cyclical shift of the above
sequence.
Next table presents the period of the generator for different multipliers between 3 and 15.
Parameter Values
Multiplier 3 4 5 6 7 8 9 10 11 12 13 14 15
Period 8 2 16 4 4 2 16 4 8 2 16 4 2
The table indicates that if the multiplier (a) is equal to 5, 9, or 13, a full period of 16 is obtained,
otherwise, partial periods of 2, 4, or 8 are achieved.
Congruential generators
(a) (b)
Congruential generators
For the case with multiplier 5, we determine the effect of constant (b) on the period of the
generator.
Next, table compares the period of generator for different b values in a range of 2 to 8.
Parameter Values
Constant 2 3 4 5 6 7 8
Period 8 16 2 16 8 16 4
The table indicates that consistently an odd b results in a full period, while only a partial period
is achieved for an even b.
Again, figures indicate that the cases with odd constants form unique shapes, while the cases
with even constants yield repeating shapes.
Congruential generators
(a) (b)
Congruential generators
Considering the above analyses for different multipliers (a), constants (b), and moduli (M), we
conclude:
A full period for a mixed congruential generator can be achieved if the three parameters have
the following properties:
a = 4N + 1 for an integer N;
b is an odd number; and,
M = 2K , for any integer K > 0.
Considering a seed of 1, Table 3.4 gives the period of random number sequences for different
multipliers in the range of 2 to 15.
Congruential generators
Multiplier 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Period - 4 - 4 - 2 - 2 - 4 - 4 - 2
Congruential generators
Algorithm Comment
read(*,*) x0 read seed
ixx(1)=x0 initialize the random number array
Do i=2,imod imod is the modulus
iab=mult*ixx(i-1)
ixx(i) = mod(iab, imod) using mod function, random number is
if(ixx(i).lt.0) then determined
ixx(i) = ixx(i)+imod To obtain a positive random number
endif Identify the end of sequence, i.e., period if
if(ixx(i).eq.x0)then the seed is separated
iperiod = i-1
goto 10
else
iperiod = 1
endif
enddo
10 continue
Congruential generators
Note
Some software, such as Matlab and MS-Excel, by default assumes that all variables are floating
point numbers. This can be troublesome in the case of random number generators because the
theory behind congruential generators is based on integer arithmetic.
In summary, a congruential generator with a reasonable period is satisfactory for most physical
simulations, because the physical system introduces randomness by applying the same random
numbers to different physical processes. This statement, however, is not always true for the
solution of a mathematical problem.
One initiates the generator by selecting j+1 random numbers (possibly from a simpler generator).
The length and the randomness (or statistical properties) of the generator depend on the values
of aj , b, and M. A special case of this type of generator is the Fibonacci generator.
is a Fibonacci generator of lags 17 and 5. The generated sequence depends on the choice of
the initial xk s, e.g., 17. Since the above Fibonacci formulation xk+1 depends upon 17 preceding
values, its period p can be large.
i.e. p = (217 − 1)2n n is the number of bits in the fractional part of xi , the mantissa.
Note
Because of the large expected period, a Fibonacci generator is a good choice for some large
problems.
To start a Fibonacci generator, it is necessary to generate the initial random numbers, e.g., 17
numbers. One approach is to represent each initial random number in its binary form
r1 r2 rm
r= + 2 + ··· + m for m ≤ n (Size of the matissa)
2 2 2
The quality of a Fibonacci formulation depends on the quality of the initial numbers or the
simpler integer generator.
A list of recommended Fibonacci generators is given in the next table:
Testing randomness
χ2 -test
The χ2 -test measures the deviation between the sample and the assumed probability distribution,
i.e., hypothesis. The formulation of the χ2 -test is given by
n
X (Ni − Npi )2
χ2 = (1)
i
Npi
Notes
This test examines the whole sampled distribution at once in comparison to a hypothetical
distribution, and is, in this sense, more general than the examination of a sample mean,
sample variance.
For large values of N, the random variable χ2 approximately follows the χ2 -distribution
density function with n − 1 degrees of freedom.
1: The chi-squared ( χ2 ) values are obtained according to Equation (1) , and then these values
are compared with the determined values given in χ2 tables
2: Commonly, we compare the estimated values, Equation (1) to predicted χ20 values that
correspond to 5% and 95% percentiles.
3: If χ2 is smaller than the value on tables of 95% at n − 1 degrees of freedom, the RNG fails
the test because it provides values that are closer than the “truth”, i.e., hypothesis predicts.
Conversely, if the χ2 value is larger than the value on tables of 5% at n − 1 degrees of
freedom,it means the RNG-generated numbers are too “variant,” beyond the expected
level.
Frequency test
In this test, one counts the number of occurrences of a particular digit in an N-digit sequence.
For example, if we consider a 10-digit sequence, then the expected occurrence of each digit is
1
10
. The χ2 values are computed to check the degree of randomness of the generator.
Serial test
Is an extension of the frequency test to pairs of digits. For any selected digit, one counts the
occurrence of every other digit following the selected digit. Again, the expected chance of
1
occurrence is 10
. The χ2 values are computed to check the degree of randomness of the
generator.
The difference is, whereas the chi-square relative frequency test examines whether individual
values are consistent with a uniform distribution, the serial test examines whether sequential,
non-overlapping pairs of random numbers have a uniform distribution.
Gap test
In the gap test, a particular digit, e.g., 0, is selected and the frequency of occurrence of nonzero
digits between successive 0’s is determined.
Poker Test
S4: Determine critical value for unspecific level of significance α and n − 1 degrees of
freedom
χ2α,n−1 = χ20.05,2 = 5.99
S5: If χ2 > χ2α,n−1 → H0 is rejected
χ20 > χ20.05,2 → H0 is rejected
The numbers are not uniformly distributed
Moment test
Given that a random number sequence should be distributed uniformly within the unit
interval, then, for a random variable η with probability density p(η) = 1, the kth moment
of η is given by :
Z 1 1
η k dη =
0 k+1
Consequently, the randomness can be tested by examining the following equality:
N
1 X k 1
η u
N i=1 i k+1
The serial correlation coefficient for a sequence of random numbers xi of length N and lag j is
given by
N N
!2
1 X 1 X
xi xi+j − xi
N i=1 N i=1
ρN, j = !2
N N
1 X 2 1 X
x − xi
N − 1 i=1 i N i=1
As random numbers are generated, they are combined, e.g., paired (x1 , x2 ), (x3 , x4 ), . . . , and
each pair is plotted as a point in a unit square. If there is any obvious pattern, e.g., striped, then
it can be concluded that the numbers are serially correlated.
Example
Poker Test
A sequence of 1,000 numbers and those numbers are 3-digits numbers they are generated and
then analysis indicates:
560 have 3 digits different
380 contain exactly one pair of like digits
60 contain 3 like digits
Based on Poker Test, test for independence use α = 0.05.
Solution
n
X (Oi − Ei )2
χ2 = = 35.55 + 250 + 44.81 = 330.36
i=1
Ei
Example
First Moment and Serial test via plotting
Remarks
Pseudorandom numbers generated via an algorithm are commonly used in practical applications.
This is because the algorithmic generators possess several favorable characteristics including
reproducibility, ease of generation, and minimal use of computer resources. However, as demon-
strated in this chapter, it is essential to examine such generators by performing numerous ran-
domness tests, as the quality of a pseudorandom number generator is highly dependent on using
the “right” parameters.