Random numbers
Random numbers
Random Number can be defined as numbers that show no consistent pattern, with each number in a
series and are neither affected in any way by the preceding number, nor predictable from it.
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.
Generating a random number series from a single seed works fine with most simulations that rely upon
generating random events under the control of probabilities (Monte Carlo simulations). However,
although the sequence of numbers generated from a given seed is randomly distributed, it is always the
same series of numbers for the same seed. Thus, a computer poker game that simply used a given seed
would always generate the same hands for each player.
What is needed is a large collection of potential seeds from which one can be more or less randomly
chosen. If there are enough possible seeds, the odds of ever getting the same series of numbers become
diminishingly small.
One way to do this is to read the time (and perhaps date) from the computer‘s system clock and generate
a seed based on that value. Since the clock value is in milliseconds, there are millions of possible values
to choose from. Another common technique is to use the interval between the user‘s keystrokes (in
milliseconds). Although they are not perfect, these techniques are quite adequate for games.
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.
3.1 Pseudorandom Number Generation
In this section we look at how random numbers may be generated by human beings for use in
simulating a system or by computer for use while simulating an event.
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, 9 irrespective of all the preceding digits in the recorded sequence.
In some simulations, we use random numbers that are between 0 and 1. For example, if you need such
numbers with four decimal digits, then you can take four at a time from the recorded sequence of
random digits, and place a decimal point in front of each group of four. To illustrate, if the sequence
of digits is 358083429261… then the four decimal placed random numbers are .3580, .8342, and .9261.
Contents
1.0 Introduction
2.0 Intended Learning Outcomes (ILOs)
3.0 Main Content
3.1 The Congreuential Method
3.2 Choice of a, c and m
3.3 RANECU Random Number Generator
3.4 Other Methods of Generating Random Numbers
3.4.1 Quadratic Congruential Method,
3.4.2 Mid-square method,
3.4.3 Mid-product Method
3.4.4 Fibonnachi Method
4.0 Self-Assessment Exercise(s)
5.0 Conclusion
6.0 Summary
7.0 Further Readings
1.0 Introduction
As has been stated earlier, if you want to write a simulation program and you neither have
a simulation language at your disposal nor a programming language with a random number
generating function, you must design and write a random number generating function and
call it whenever you need it.
Classical uniform random number generators have some major defects, such as, short
period length and lack of higher dimension uniformity. However, nowadays there are a
class of rather complex random number generators which are as efficient as the classical
generators which enjoy the property of a much longer period and of higher dimension
uniformity.
24
3.0 Main Content
3.1 The Congruential Method
The Congruential Method is widely used. The method is based on modulus arithmetic,
which we now discuss briefly.
We say that two numbers x and y are congruent modulo m if (x-y) is an integral multiple
of m. Thus we can write: x = y(modulo m)
For example, let m = 10, then we can write:
13 ≡ 3 (modulo 10)
84 ≡ 4 (modulo 10)
The congruential method generates random numbers by computing the next random
number from the last random number obtained, given an initial random number say, X0,
called the seed.
From the above formula, it follows that the random number generated must be between 0
and (m-1) since MOD (modulo) produces remainder after division. Hence the above
formula will produce the remainder after dividing (aXn + C ) by m. So to generate a
random number between p and m we use:
The multiplicative congruential method is very handy. It is obtained using the general
formula:
rn = arn-1 (modulo m)
where the parameters a, m and the seed r0 are specified to give desirable statistical
properties of the resultant sequence. By virtue of modulo arithmetic, each rn must be one of
the numbers 0,1,2,3… m-1. Clearly, you must be careful about the choice of a and r0. The
25
values of ‗a‘ and r0 should be chosen to yield the largest cycle or period, that is to give the
largest value for n at which rn = r0 for the first time.
Example 4
To illustrate the technique, suppose you want to generate ten decimal place numbers u1,
u2, u3,…. It can be shown that if you use
un = rn x 10-1
where rn = 100003rn-1 (modulo 1010), and r0 = any odd number not divisible by 5,
then the period of the sequence will be 5 x 108, that is rn = r0 for the first time at n = 5 x
108 and the cycle subsequently repeats itself.
n Xn+1 = (5Xn+7)mod 8
0 X1 = (5*X0+7)mod 8 = (5*X4+7)mod 8 = 27 mod 8 = 3
1 X2 = (5*X1+7)mod 8 = (5*X3+7)mod 8 = 22 mod 8 = 6
2 X3 = (5*X2+7)mod 8 = (5*X6+7)mod 8 = 37 mod 8 = 5
3 X4 = (5*X3+7)mod 8 = (5*X5+7)mod 8 = 32 mod 8 = 0
4 X5 = (5*X4+7)mod 8 = (5*X0+7)mod 8 = 7 mod 8 = 7
5 X6 = (5*X5+7)mod 8 = (5*X7+7)mod 8 = 42 mod 8 = 2
6 X7 = (5*X6+7)mod 8 = (5*X2+7)mod 8 = 17 mod 8 = 1
7 X8 = (5*X7+7)mod 8 = (5*X1+7)mod 8 = 12 mod 8 = 4
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 RND function of BASIC.
In his book, The Art of Computer Programming, Donald Knuth presents several rules for
maximizing the length of time before the random number generator comes up with the
same value as the seed. This is desirable because once the random number generator comes
up with the initial seed, it will start to repeat the same sequence of random numbers (which
26
will not be so random since the second time around we can predict what they will be).
According to Knuth's rules, if M is prime, we can let C be 0.
The LCM defined above has full period if and only if the following conditions are
satisfied:
a) m and c are relatively prime
b) If q is a prime number that divides m , then q divides a-1
c) If 4 divides m, then 4 divides a-1
Therefore, the values for a, c and m are not generated randomly, rather they are carefully
chosen based on certain considerations. For a binary computer with a word length of r bits,
the normal choice for m is m = 2r-1. With this choice of m, a can assume any of the values
1, 5,9,13, and c can assume any of the values 1, 3, 5, 7… However, experience shows that
the congruential method works out very well if the value of a is an odd integer not divisible
by either 3 or 5 and c chosen such that c mod 8 = 5 (for a binary computer) or c mod 200 =
21 (for a decimal computer).
Example 5
Develop a function procedure called RAND in QBASIC which generates a random number
between 0 and 1 using the mixed congruential method. Assume a 16-bit computer.
Solution
FUNCTION RAND (SEED)
CONST M = 32767, A = 2743, C = 5923
IF SEED < 0 THEN SEED = SEED + M
SEED = (A* SEED + C) MOD M
RAND = SEED/M
END FUNCTION
Note that in the main program that references the above function in (a), the TIMER function
can be used to generate the SEED to be passed to the function RAND as illustrated in
example 2.
Example 5
Write a program that can generate that can generate 20 random integer number distributed
between 1 and 64 inclusive using mixed congruential method.
Solution
QBASIC
27
SEED = RAND (SEED) ‗Call of function RAND PRINT SEED: SPC(2)
NEXT K%
END ‗End of main program
PROGRAM RANDNUM
COMMON SEED
CLS ‗Clear screen
DO 50 K = 1, 25
WRITE (*, 5)
FORMAT(/)
50 CONTINUE
WRITE(*,*) ‗Enter the seed‘
READ(*,*) SEED
DO 30 J = 1, 20
SEED = RAND
WRITE (*, 6) SEED
6 FORMAT (I4)
30 CONTINUE
END
FUNCTION RAND
COMMON SEED
PARAMETER (M = 64, A = 27, C =13)
IF (SEED.LT.0) SEED = SEED + M
HOLD = (A*SEED + C)
SEED = AMOD (HOLD,M) + 1
RAND = SEED
RETURN END
28
failing which a default set is employed. If supplied, these three seeds, in order, should lie in
the ranges [1,32362], [1,31726] and [1,31656] respectively. The program is given below.
29
3.4.1 The Quadratic congruential method
This method uses the formula:
Xn=1 = (dX 2 + cX + a) modulo mn n
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.
30
a. A sequence of 10 one-digit random numbers given that
Xn+1 ≡ (Xn + 3)(modulo 10) and X0 = 2
b. A sequence of eight random numbers between 0 and 7 given that
Xn+1 ≡ (5Xn + 1)(modulo 8) and X0 = 4
c. A sequence of two-digit random numbers such that
Xn+1 ≡ (61Xn + 27)(modulo 100) and X0 = 40
d. A sequence of five-digit random numbers such that
Xn+1 ≡ (21Xn + 53)(modulo 100) and X0 = 33
5. Define a methods period and state how to improve a period. Show two examples of
such improvement.
6. Consider the multiplicative congruential method for generating random digits. In
each part below, assume modulo 10 arithmetic and determine the length of the
cycle:
a. Let a = 2 and r0 = 1, 3 and 5
b. Let a = 3 and r0 = 1,2 and 5
5.0 Conclusion
In this unit, you have been introduced to Random Numbers generation. You have also
learnt how to design random number generator.
6.0 Summary
What you have learnt in this unit concern:
The Congruential methods of generating random numbers,
The use of QBasic RND function to simulate randomness,
The other Random number generating methods,
The properties of good random number generator.
31