You are on page 1of 10

Generating

random NUMBER
A contradiction?

 Yes, because “generating” means done purposefully,


but “random” is unpredictable
 We generate a sequence of random numbers
(=stream) : X0, X1, X2, …
while Xi is integer and 0 ≤ Xi < M
that “looks” random: no test can disprove that
Prob{Xi|Xi-1,Xi-2,…X0} = 1/M (1)
 The stream is pseudo-random
Method for generating stream

Xi+1 = (aXi + b) mod M (2)

when X0 (=seed), a, b and M are given constants, v mod M


is the remainder after dividing v by M
This is the Linear Congruential Generator (LCG)
(There are many other generators, but this one is used in commercial software)

Example: seed=0, a=2, b=1, M=5: stream=


0,1,3,2,0,1,3,2,0,1,3,2,0,1,3,2,0,1,3,2,…
Does it look random?
Limitations of LCG and remedies - i

Condition (1) is easily disproved (0,1,3,2 repeats)


Remedy: M should be big (say 231~2×109), and
choice of M, a and b should be such that for
short sequences (relative to M) condition (1)
cannot be rejected by statistical tests
(It can be shown that such M, a and b exist – they are imbedded in
commercial software and do not need to be tested)
Limitations of LCG and remedies - ii

Plot of Xi vs. Xi+1 in LCG with M=64,a=37,c=1:

Is it random when all points are on parallel straight lines?


Lattice property – another “inborn malady” of LCG
Again, appropriate M, a, b can minimize damage
GENERATE random NUMBER FIT TO A
PDF
The inverse method

Theorem: Let Y be a Xi from the stream


continuous random
variable with probability
Ui = Xi/M
function Ui is uniformly distributed
between 0 and 1
F(y) = Prob{Y  y}.

Then F(Y) is uniformly


Yi = F-1(Ui)
distributed between 0 and where F-1 is the inverse
1. probability function

7
Idea of the inverse method
Advantages & disadvantages

 Disadvantage: No analytic form is known for most


interesting random variables.
 Remedy: there are good approximations
 Disadvantage: Works only for continuous random
variables
 Remedy: an extension exists for discrete case
 Advantage: Y is a monotone function of a single X
 Importance: the only method appropriate for variance
reduction technique
(There are other methods, but all of them are without the advantage)
Inverse method by Excel functions

RAND() returns next Ui, ~INV() is the appropriate F-1

Distribution Variate generation in Excel

Beta =BETAINV(RAND(),shape1,shape2,A,B)

Gamma =GAMMAINV(RAND(),shape,scale)

Normal =NORMINV(RAND(),mean,stdDev)
Exponential =-expectedTime*LN(1-RAND())

10

You might also like