You are on page 1of 35

Random Number Generation

RNG

2020A
Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Content

1 Random number generation approaches

2 Pseudorandom number generators (PRNGs)


Congruential generators
Multiple recursive generator

3 Testing randomness

4 Examples for PRNG test

5 Remarks

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Random number generation approaches

There are two common approaches for generating random numbers:


Experimental (look-up tables, online)
Algorithmic (or deterministic)

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Random number generation approaches


Experimental

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Random number generation approaches


Algorithmic

An algorithm is used to generate random numbers.


This approach, because of its deterministic nature, is commonly referred to as pseudorandom
number generator and its associated numbers are referred to as pseudorandom numbers.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Random number generation approaches


Advantages/Disadvantages

The important factors for selecting the “right” approach include:


1 Randomness
2 Reproducibility
3 Length of the sequence of random numbers
4 Computer memory
5 Generation time
6 Computer time

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Random number generation approaches


Comparison of experimental and algorithmic random number generators

Approach Randomness Reproducibility Period Computer Generation Computer


Memory Time Time
Experimental Good Yes n/a Large Long Short
(Table)
Experimental Good No n/a Small n/a Long
(Online)
Algorithmic Should Yes Limited Small n/a Short
satisfy the
statistical
tests

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Congruential generators

Pseudo-random number generators (PRNGs)

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

A congruential generator is an integer generator proposed by D. H. Lehmer (1951). It uses the


following formulation

xk+1 = (axk + b) mod M, for b < M < IMAX

where x0 (seed), a, b, and M are given integers. IMAX is the largest integer represented by a
computer.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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

xk+1 = (5xk + 1) mod 16


x0 = 1
x1 = 6 mod 16 = 6
x2 = 31 mod 16 = 15
x3 = 76 mod 16 = 12
x4 = 61 mod 16 = 13

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Congruential generators

(a) (b)

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Congruential generators

(a) (b)

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Now, we consider a multiplicative congruential generator by setting the constant parameter b to


zero, i.e.,
xk+1 = (axk ) mod 16

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Congruential generators

An algorithm for a multiplicative random number generator

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

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Multiple recursive generator

Multiple recursive generator

A group of PRNGs referred to as multiple recursive generators can be expressed by

xk+1 = (a0 xk + a1 xk−1 + · · · + aj xk−j + b) mod M

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Multiple recursive generator

The Fibonacci generator

A Fibonacci generator is a floating point generator. It is characterized by computing a new


number by a combination (difference, sum, or product) of two preceding numbers.
For example:
xk = (xk−17 − xk−5 ) mod M

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Multiple recursive generator

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

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Multiple recursive generator

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:

Generator Expected period


xk = xk−17 − xk−5 (217 − 1) × 224 = 2.2 × 1012

xk = xk−17 + xk−5 (217 − 1) × 224 = 2.2 × 1012

xk = xk−31 − xk−13 (231 − 1) × 224 = 3.6 × 1016

xk = xk−97 − xk−33 (297 − 1) × 224 = 2.7 × 1036

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Testing randomness

A pseudo-random number generator is acceptable if it passes a series of randomness tests. There


are numerous randomness tests, which look at various parameters for examining independence
and uniformity of a random number sequence.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

χ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

where p1 , p2 , . . . , pn is a set of hypothetical probabilities associated with N events falling into


ncategories with observed frequencies of N1 , N2 , . . . , Nn .

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Procedure for the use of χ2 -test

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

For a single gap, the expected frequency is 9/100.

Again, the χ2 values are used to measure the quality of generator.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Poker Test

Consider only n digits of the RNs


RNs are rounded off to n digits and classified as:
a) All digits different
b) All digits identical
c) Exactly one pairs of identical digits

Focuses on internal digits of given RNs


For n=3
a) Probability that all digits are different
P(2nd digit different than 1st ) · P(3rd digit different than 1st & 2nd ) = 0.8 · 0.9 = 0.72
b) Probability that all digits are identical
P(2nd digit same as 1st ) · P(3rd digit same as 1st ) = 0.1 · 0.1 = 0.01
c) Probability of getting one pair of identical digits
1 − [P(3 different digits) + P(3 like digits)] = 1 − (0.72 + 0.01) = 0.27

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Moment test

The kth order moment of y is defined by:


Z b
[kth moment of y] = yk p(y) dy for a ≤ y ≤ b
a

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

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Serial correlation test

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

If xi and xi+j are independent and N is large,  (ρN, j ) should follow


 then the correlation coefficient

a normal distribution with mean, − N1 , and standard deviation √1 .
N

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Serial test via plotting

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Solution

S1: Define hypothesis for testing independence as :


H0 : Ri ∼ independently
H1 : Ri 6∼ independently
S2: Generate Frequency distribution table for 3 combinations and apply Chi-Square Test
(Oi −Ei )2
Combination Obs. Freq Expected Freq Ei
3 diff digits 560 720 35.55
3 like digits 60 10 250
1 pair digits 380 270 44.81
S3: Compute test stats

n
X (Oi − Ei )2
χ2 = = 35.55 + 250 + 44.81 = 330.36
i=1
Ei

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

Example
First Moment and Serial test via plotting

For a mixed congruential generator given by xk+1 = (axk + b) mod M:


Case Seed a b M Period Period/M (%) Mean
1 1 65539 1 224 4194304 25% 0.500
2 1 65541 1 224 16777216 100% 0.500
3 1 16333 1 224 16777216 100% 0.500

(a) Case 1 (b) Case 2 (c) Case 3

Random Number Generation


Random number generation approaches Pseudorandom number generators (PRNGs) Testing randomness Examples for PRNG test Remarks

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.

Anyone who considers arithmetical


methods of producing random digits is, of
course, in a state of sin.–

John von Neumann

Random Number Generation

You might also like