Random Number Generation
Random Number Generation
0 RANDOM NUMBERS
4.1. Introduction
The use of Random numbers lies at the heart modeling and simulation. Computer
applications such as simulations, games, graphics, etc., often need the ability to generate
random numbers for such application.
The quality of a random number generator is proportional to its period, or the number of
random numbers it can produce before a repeating pattern sets in. In large-scale simulations,
different algorithms (called shift-register and lagged-Fibonacci) can be used, although these
also have some drawbacks, combining two different types of generators may produce the best
results.
1
One way to get random digits is to simply start with an arbitrary number with a specified
number of digits, for example 4 digits. The first number is called the seed. The seed is
multiplied by a constant number of the same number of digits (length), and the desired number
of digits is taken off the right end of the product. The result becomes the new seed.
It is again multiplied by the original constant to generate a new product, and the process is
repeated as often as desired. The result is a series of digits that appear randomly distributed as
though generated by throwing a die or spinning a wheel. This type of algorithm is called a
congruential generator
The so-called true random number generators extract random numbers from physical
phenomena such as a radioactive source or even atmospheric noise as detected by a radio
receiver.
PRN is a set of values or elements that is statistically random, but it is derived from a known
starting point and is typically repeated over and over. It is called "pseudo" random, because the
algorithm can repeat the sequence, and the numbers are thus not entirely random.
What we usually do is to take for instance ten pieces of papers and number them
0,1,2,3,4,5,6,7,8, and 9 , fold and place them in a box. Shake the box and thoroughly mix the
slips of paper. Select a slip; then record the number that is on it. Replace the slip and repeat this
procedure over and over.
The resultant record of digits is a realized sequence of random numbers. Assuming you
thoroughly mix the slips before every draw, the nth digit of the sequence has an equal or
uniform chance of being any of the digits 0,1,2,3,4,5,6,7,8, and 9 irrespective of all the
preceding digits in the recorded sequence.
2
True random number generators (TRNGs):
Truly random is defined as exhibiting “true” randomness. This type uses a physical source of
randomness to provide truly unpredictable numbers. TRNGs are mainly used for cryptography,
because they are too slow for simulation purposes. Many true random number generators are
hardware solutions that you plug to a computer. The usual method is to amplify noise generated
by a resistor (Johnson noise) or a semi-conductor diode and feed this to a comparator or Schmitt
trigger. Once you sample the output, you get a series of bits which can be used to generate
random numbers. True random number generators can be used for research, modeling,
encryption, lottery prediction and parapsychological testing, among many other uses.
Most programing languages have built-in random number generators (Excel, Matlab all have it).
In Matlab, the command rand (1) returns a random number between 0 and 1 assuming uniform
distribution. We can build other random variables using rand. For example, to get a random
number between a and b we can use a+rand(1)(b − a). To get a 0 or 1 on a random way in
Matlab, you can use round(rand(1)). The function round(x) returns 0 of x ≤ 1/2 and returns 1 if x
> 1/2.
The function round is built-in in Matlab so you can use it without entering the M-file round
given below.
function y=round(x)
if x<=1/2
y=0;
else y=1;
3
end
Microsoft Excel has a numeric function named RAND, which generates random
numbers between 0 and 1. Each time RAND is executed, a pseudo random number
between 0 and 1 is generated. RAND returns an evenly distributed random real
number greater than or equal to 0 and less than 1. A new random real number is
returned every time the worksheet is calculated. RAND uses the following syntax:
RAND (). The RAND function syntax has no arguments.
Using RAND function at any time will always generate the same sequence of
pseudo random numbers unless we vary the random number seed using the Excel
statement: To generate a random real number between a and b, use: RAND ()*(b-a)+a
If you want to use RAND to generate a random number but don't want the numbers to
change every time the cell is calculated, you can enter =RAND() in the formula bar,
and then press F9 to change the formula to a random number.
Example
Table 1: Formula and Description
=RAND() A random number greater than or equal to 0 and less than 1 (varies) varies
=RAND()*100 A random number greater than or equal to 0 but less than 100 (varies) varies
Now the function RAND generates a random number between 0 and 1. Specifically, the
random variable X is in the range: 0 < X < 1. The expression X= RAND ()*6. Will generate a
number in the range: 0 < X < 6.
– the "modulus"
– the "multiplier"
– the "increment"
– the "seed" or "start value"
The random integers are being generated [0,m-1], and to convert the integers to random numbers:
Ri = Xi/m
Example 1
Solution
The Xi and Ri values are:
X1 = (17*27+43) mod 100 = 502 mod 100 = 2, R1 = 0.02;
X2 = (17*2+43) mod 100 = 77, R2 = 0.77;
X3 = (17*77+43) mod 100 = 52, R3 = 0.52;
X3 = (17*52+43) mod 100 = 27, R4 = 0.27;
Example
eg x0=1, a=2, c=3, m=5 (ans =0)
5
x1=0
x2=3
x3=4
x4=1
Example
For example, the sequence obtained
•when X0 = a = c = 7, m = 10
•Required: Generate six random numbers
Solution
Example
As an example, using our Linear congruential formula
•Xn+1 = (aXn+c) (modulo m),
•And suppose m = 8, a = 5, c = 7 and X0 (seed) = 4
•Generate 7 random numbers using congruential formula above
6
Example
For example, the sequence obtained when m = 10 and X0 = a = c = 7 is 7,6,9,0,7,6,9,0,………..
As this example shows, the sequence is not always "random" for all choices of m, a, c, and X 0.
This illustrates the fact that the congruential sequences always "get into a loop"; i.e., there is
ultimately a cycle of numbers that is repeated endlessly. This property is common to all
sequences having the general form Xn+1 = f(Xn); the repeating cycle is called the period;
sequence (2) has a period of length 4. A useful sequence will of course have a relatively long
period.
Exercise
7
4.8.2. The Quadratic congruential method
This method uses the formula:
Xn=1 = (dX 2+ cXn + a) n modulo m
Where d is chosen in the same way as c and m should be a power of 2 for the method to
yield satisfactory results.
This method is demonstrated as shown in Table below by assuming the initial seed as 8765
Example 2:
Given the seed X0=7182, generate 7 random numbers using Mid square method
8
Limitations
The mid-square method is rarely used these days as it has the tendency to degenerate
rapidly.
Also, if the number zero is ever generated, then all subsequent numbers generated will
be zero.
Furthermore, the method is slow when simulated in the computer since many
multiplications and divisions are required to access the middle four digits.
There is no relationship between the initial seed and the length of the sequence of
random numbers
9
Problems with Fibonacci Generator (FG):
The initialization of FGs is a very complex problem. The output of FGs is very sensitive
to initial conditions, and statistical defects may appear initially but also periodically in the
output sequence unless extreme care is taken.
Another potential problem with FGs is that the mathematical theory behind them is
incomplete, making it necessary to rely on statistical tests rather than theoretical
performance
From the foregoing discussions, it is obvious that the last three methods – mid-square, mid-
product and Fibonacci are of historical significance and have detrimental and limiting
characteristics.
Exercises
1. Write an Excel program using Quadratic congruential method to generate 15 random
integer numbers between 1 and 50.
2. Produce a table of random numbers using multiplicative congruential method, using a
=5, m =8 and X0 = 4. Draw an inference from your solution.
3. Write a Excel function that can be referenced as computer random number between 30
and 100 using mixed congruential method.
4. Use the mixed congruential method to generate the following sequences of random
numbers:
a. A sequence of 10 one-digit random numbers given that
X
n+1 (Xn + 3)(modulo 10) and X0 = 2
b. A sequence of eight random numbers between 0 and 7 given that
10
X
n+1 (5Xn + 1)(modulo 8) and X0 = 4
c. A sequence of two-digit random numbers such that
X
n+1 (61Xn + 27)(modulo 100) and X0 = 40
d. A sequence of five-digit random numbers such that
X
n+1 (21Xn + 53)(modulo 100) and X0 = 33
u = r x 10-1
n n
where r = 100003r (modulo 1010), and r = any odd number not divisible by 5, then the
n n-1 0
period of the sequence will be 5 x 10 , that is r = r for the first time at n = 5 x 108 and the
8
n 0
cycle subsequently repeat itself. As an example, using our mixed congruential formula
Xn+1 = (aXn+c) (modulo m),
And suppose m = 8, a = 5, c = 7 and X0 (seed) = 4 we can generate a random sequence
of integer numbers thus:
Table 2: Random sequence of integer numbers
n Xn+1 = (5Xn+7)mod 8
0 X1 = (5*X0+7)mod 8 = (5*X4+7)mod 8 = 27 mod 8 = 3
Note that the value of X8 is 4, which is the value of the seed X0. So if we compute X9, X10, etc
the same random numbers 3, 6,5,0,7,2,1,4 will be generated once more. Note also that if we
divide the random integer values by 8, we obtain random numbers in the range 0 < Xn+1 < 1
which is similar to using the RAND function of Excel
11
Simulation: When a computer is being used to simulate natural phenomena, random
numbers are required to make things realistic. Simulation covers many fields, from the
study of nuclear physics (where particles are subject to random collisions) to
operations research (where people come into, say, an airport at random intervals).
Sampling: It is often impractical to examine all possible cases, but a random sample
will provide insight into what constitutes "typical" behavior.
Computer programming: Random values make a good source of data for testing the
effectiveness of computer algorithms.
Decision making: Sometimes it is important to make a completely "unbiased decision;
this ability is occasionally useful in computer algorithms, for example in situations
where a fixed decision made each time would cause the algorithm to run more slowly.
Randomness is also an essential part of optimal strategies in the theory of games.
Recreation: Rolling dice, shuffling decks of cards, spinning roulette wheels, etc., are
fascinating pastimes for just about everybody.
In a true random sequence the number of 1’s & 0’s should be about the same. This test checks
whether this in true or not.
12
To achieve this, the test uses the complementary error function (Erfc) in- Matlab or Java. The
following steps are involved.
Example
Consider 1011010101
Then Sn= 1-1+1+1-1+1-1+1-1+1= 2
Sobs =2/sqrt(10) = 0.6324
For α= 0.01, we obtain that P- value = Erfc (0.6324)= 0.5271> 0.01. thus the sequence is
accepted as being random.
The serial test determines whether the number of times each of this combination occurs is
uniformly distributed.
The runs test examines the arrangement of numbers in a sequence to test the hypothesis of
independence.
A run is defined as a succession of similar events preceded and followed by a different event.
E.g. in a sequence of tosses of a coin, we may have
13
HTTHHTTTHT
The first toss is preceded and the last toss is followed by a "no event". This sequence has six
runs, first with a length of one, second and third with length two, fourth length three, fifth and
sixth length one.
A few features of a run:
two characteristics: number of runs and the length of run
an up run is a sequence of numbers each of which is succeeded by a larger number; a
down run is a sequence of numbers each of which is succeeded by a smaller number
If a sequence of numbers have too few runs, it is unlikely a real random sequence. E.g. 0.08,
0.18, 0.23, 0.36, 0.42, 0.55, 0.63, 0.72, 0.89, 0.91, the sequence has one run, an up run. It is not
likely a random sequence.
If a sequence of numbers have too many runs, it is unlikely a real random sequence. E.g. 0.08,
0.93, 0.15, 0.96, 0.26, 0.84, 0.28, 0.79, 0.36, 0.57. It has nine runs, five up and four down. It is
not likely a random sequence
For example, in the stock market, run test of randomness is applied to know if the stock price of
a particular company is behaving randomly, or if there is any pattern.
Autocorrelation means that the data has correlation with its lagged value. Hence this method is
used to test whether or not the data has correlation with the lagged value.
vi)Poker Test
The poker test for independence is based on the frequency in which certain digits are repeated in
a series of numbers.
14
This considers groups of k members of the sequence and count the number of distinct values
represented in each group (e.g., a hand of 5 cards falls into one of a number of groups, such as
one pair, two pairs, three of a kind, etc)
For example 0.255, 0.577, 0.331, 0.414, 0.828, 0.909, 0.303, 0.001... In each case, a pair of like
digits appears in the number.
The basic ingredient needed for every method of generating random variates from any
distribution or random process is a source of Independent and identically distributed (IID) or
uniform distribution 𝑈(0,1) random numbers. There are several alternative algorithms for
generating random variates from a given distribution.
15
algorithm should be carefully weighed against the extra effort needed to understand and
implement it.
iv. Robustness: Some algorithms rely on a source of random variables from distributions
other that 𝑈(0,1). Also a given algorithm may be efficient for some parameter values but
costly for others. A good algorithm should be efficient for a wide range of parameter
values (robust).
v. Ability to use variance-reduction techniques: Two commonly used techniques are:
-Common random variables
-Antithetic variates (contrast to common)
These techniques require synchronization of the basic 𝑈(0,1) input random variates used in the
simulation of the system under study. This synchronization is more easily accomplished for
certain types of random variate generation algorithms. In particular, the universe transform
method can be very helpful in facilitating the desired synchronization and variance reduction.
i) Direct method
Directly uses the definition of the distribution. We have inbuilt functions that directly generate
the desired random number.
e.g rand: generates uniform random variate on (0,1)
randn; generate random numbers from standard normal distribution
binornd: generate random numbers from binomial distribution e.t.c
To generate random numbers between interval say (a,b) different from (0,1) say (3,8) we have
x= floor (rand (1,8)* 5+3)
in general we have x= floor (rand (1,y)* (b-a)+a)
16
The method is applicable only to cases where the cumulative density function (cdf) can be
inversed analytically.
Assume that we wish to generate stochastic variates from a probability density function (pdf) f
(x).
Let F(x) be its cumulative density function. We note that F(x) is defined in the region (0,1)
We explore this property of the cumulative density function to obtain the following simple
stochastic variants generator.
First generate a random number r which we set equal to F(x) . F(x)=r
The quantity x is then obtained by inverting F. i.e x= F-1(r)
Example
We wish to generate random variates with pd of f(x)= 2x . 0≤x≤1
First calculate the cdf F(x) i.e
F(x) =
Let r be a random number , we have
r=x2
x=√r
18