You are on page 1of 30

Chapter 7

Random-Number
Generation
Dr. Banu al

Random Number
Generators
Without random numbers, we cannot do
Stochastic Simulation
Most computer languages have a
subroutine, object or function generating
random numbers (uniformly distributed)
Simulation languages provide more than
that (you can get random samples from
many distributions)
How do they generate it?
How can we test their randomness?

Pseudo-Random Numbers
Approach:
Arithmetically
generation
(calculation) of random numbers
Pseudo, because generating numbers using
a known method removes the potential for true
randomness. (Because these numbers are
produced using a deterministic algorithm)
Goal: To produce a sequence of numbers
in [0,1) that simulates, or imitates, the
ideal properties of random numbers (RN).

Pseudo-Random Numbers

Pseudo-Random Numbers
Important properties of good random
number routines:
Fast
Portable to different computers
Have sufficiently long cycle
Replicable
Verification and debugging
Use identical stream of random
numbers for different systems

Pseudo-Random Numbers
Problems when generating pseudorandom numbers
The generated numbers might not be
uniformly distributed
The
generated
numbers
might
be
discrete-valued instead of continuousvalued
The mean of the generated numbers
might be too high or too low
The variance of the generated numbers
might be too high or too low

Pseudo-Random Numbers
There might be dependence:
Autocorrelation between numbers
Numbers successively higher or
lower than adjacent numbers
Several numbers above the mean
followed by several numbers below the
mean

Generating Random
Numbers
1. Midsquare method
2. Linear Congruential Method (LCM)
3. Combined Linear Congruential
Generators (CLCG)
4. Random-Number Streams

Midsquare method
First

arithmetic generator: Midsquare


method
von Neumann and Metropolis in 1940s

The Midsquare method:


Start with a four-digit positive integer
Compute:2 = x to obtain an integer
with up to eight digits
Take the middle four digits for the
next four-digit number

Midsquare method
Example: = 7182

Problem:
Generated
numbers tend to

Linear Congruential Method


To produce a sequence of integers X1, X2, between 0
and m-1 by following a recursive relationship:
and to convert
the integers to random
numbers:

Assumption: m > 0 and a < m, c < m, X0 < m


The selection of the values for a, c, m, and X 0
drastically affects the statistical properties and the
cycle length. The initial value X0 is called the
seed, a the constant multiplier, c the
increment, and m the modulus
The random integers Xi are being generated in [0, m1]

Note:

Linear Congruential Method:


Example

If c 0, the form is called the mixed congruential method


if c = 0, the form is called the multiplicative congruential method

The choice of the parameters affect the statistical


properties and the cycle length
Xi {0, 1, ..., m-1}
Ri [0, (m-1)/m]

Use X0 = 27, a = 17, c = 43, and m = 100. The X i and Ri


values are:

X1
X2
X3
X4

=
=
=
=

(1727+43) mod 100 = 502 mod 100 = 2


(172 +43) mod 100 = 77
(1777+43) mod 100 = 52
(1752+43) mod 100 = 27

R 1 = 0.02
R 2 = 0.77
R 3 = 0.52
R 4 = 0.27

Linear Congruential Method:


Example
Example 7.3
Use a = 13, c = 0, and
m = 64
The period of the generator
is very low
Seed X0 influences
the sequence

Linear Congruential Method:


Characteristics of a good Generator
Maximum Density
The values assumed by Ri , i=1,2, leave no large
gaps on [0,1]
Problem: Instead of continuous, each Ri is discrete
Solution: a very large integer for modulus m
Approximation appears to be of little consequence

Maximum Period
To achieve maximum density and avoid cycling
Achieved by proper choice of a, c, m, and X 0
For m = 2b, c 0 and relatively prime to m and a =
1+4k, the longest possible period P = m.
For m = 2b, c = 0, X0 (seed) odd, and a = 3+8k or a
= 5+8k, the longest possible period P = m/4
For m a prime number, etc.. P = m-1

Combined Linear Congruential


Generators
To simulate more complex systems, the
simulation runs need to go through larger
numbers of elementary events
This means that kind of simulation runs
have to use more random numbers
In order to have healthy runs, pseudo-random
generators with longer periods are needed
(So that cycles can be avoided during the
run)
It is possible to combine two or more
multiplicative congruential generators in such
a way that the combined generator has good
statistical properties and a longer period

Combined Linear Congruential


Generators

Approach: Combine two or more


multiplicative congruential generators.

Combined Linear Congruential


Generators

Combined Linear Congruential


Generators Example 7.5.

Random-Numbers Streams and


Seeds

The seed for a random-number generator:

Is the integer value X0 that initializes the random-number


sequence.
Any value in the sequence can be used to seed the generator.

A random-number stream:
Refers to random numbers obtained by using a starting
seed
If the streams areS bvalues
X b (i 1) apart, then stream i could defined by
i
starting seed:
Older generators: b = 105; Newer generators: b = 1037.

A single random-number generator with k streams can


act like k distinct virtual random-number generators
To compare two or more alternative systems.
Advantageous to dedicate portions of the pseudo-random
number sequence to the same purpose in each of the simulated
systems.

Pseudo-Random Number Generation


in ARENA
With todays computing power, the cycling
can occur in minutes of simulation with with a
cycle length of 2 billion (2.1x109)
ARENA thus uses a combined multiple
recursive generator which combines two
separate generators
An (1403580 An 2 810728 An 3 ) mod 4294967087
Bn (527612 Bn 1 1370589 Bn 3 ) mod 4294944443
X n ( An Bn ) mod 4294967087

Pseudo-Random Number Generation


in ARENA
The cycle length of this generator is
3.1x1057
This is inexhaustible with the current
computing speeds
Just to generate them would take 10 40
millennia (thousand years) on a 2GHz PC
The Arena generator has facility to split this
cycle into 1.8x109 separate streams, each of
length 1.7x1038
Each stream is further subdivided into
2.3x1015 separate substreams of length
7.6x1022 apiece

Tests for Random Numbers


Two categories:
Testing for uniformity:
H0: Ri ~ U[0,1]
H1: Ri ~ U[0,1]
Failure to reject the null hypothesis, H0, means that evidence
of non-uniformity has not been detected.

Testing for independence:


H0: Ri ~ independently
H1: Ri ~ independently
Failure to reject the null hypothesis, H0, means that evidence
of dependence has not been detected.

Level of significance the probability of rejecting H0


when it is true:
= P(reject H0|H0 is true)

Tests for Random Numbers


When to use these tests:
If a well-known simulation languages or random-number
generators is used, it is probably unnecessary to test
If the generator is not explicitly known or documented,
e.g., spreadsheet programs, symbolic/numerical
calculators, tests should be applied to many sample
numbers.

Types of tests:
Theoretical tests: evaluate the choices of m, a, and c
without actually generating any numbers
Empirical tests: applied to actual sequences of numbers
produced.

Frequency Tests
Test of uniformity
Two different methods:
Kolmogorov-Smirnov test
Chi-square test

Chi-square test

Chi-square test

Tests for Autocorrelation

Tests for Autocorrelation

Example
27

Summary

You might also like