Random Number Generator
Random Number Generator
Generation
26-1
Overview
Desired properties of a good generator
Linear-congruential generators
Tausworthe generators
Survey of random number generators
Seed selection
Myths about random number generation
26-2
Random-Number Generation
Random Number = Uniform (0, 1)
Random Variate = Other distributions
= Function(Random number)
26-3
A Sample Generator
For example,
26-4
Terminology
Seed = x0
Pseudo-Random: Deterministic yet would pass randomness
tests
Fully Random: Not repeatable
Cycle length, Tail, Period
26-5
26-6
26-7
Linear-Congruential Generators
Equivalently,
a = multiplier
m = modulus
26-8
26-9
26-10
26-11
26-12
Multiplicative LCG
Two types:
m = 2k
m 2k
26-13
26-14
Example 26.1a
26-15
Example 26.1b
26-16
26-17
Example 26.2
26-18
Schrage's Method
PRN computation assumes:
No round-off errors, integer arithmetic and no overflows
Can't do it in BASIC
Product a xn-1 > Largest integer Overflow
Identity:
Where:
And:
Here, q = m div a, r = m mod a
`A div B' = dividing A by B and truncating the result.
For all x's in the range 1, 2, , m-1, computing g(x) involves
numbers less than m-1.
If r < q, h(x) is either 0 or 1, and it can be inferred from g(x);
h(x) is 1 if and only if g(x) is negative.
26-19
Example 26.3
26-20
26-21
26-22
Tausworthe Generators
26-23
Characteristic polynomial:
26-24
Example 26.4
x7+x3+1
Using D operator in place of x:
Or:
Or:
26-25
Starting with b0 = b1 = L = b6 = 1:
26-26
Combined Generators
1. Adding random numbers obtained by two or more generators.
wn=(xn+yn) mod m
For example, L'Ecuyer (1986):
26-41
Use:
26-42
26-43
a)
b)
c)
d)
e)
Algorithm M:
Fill an array of size, say, 100.
Generate a new yn (between 0 and m-1)
Index i=1+100 yn/m
ith element of the array is returned as the next random number
A new value of xn is generated and stored in the ith location
26-44
26-45
26-46
26-47
26-48
26-49
Seed Selection
Multi-stream simulations: Need more than one random
stream
Single queue Two streams
= Random arrival and random service times
1. Do not use zero. Fine for mixed LCGs.
But multiplicative LCG or a Tausworthe LCG will stick at
zero.
2. Avoid even values. For multiplicative LCG with modulus
m=2k, the seed should be odd. Better to avoid generators that
have too many conditions on seed values or whose
performance (period and randomness) depends upon the seed
value.
3. Do not subdivide one stream.
26-50
26-51
Table of Seeds
26-52
26-53
Myths (Cont)
4. Some seeds are better than others. May be true for some.
26-54
Myths (Cont)
5. Accurate implementation is not important.
RNGs must be implemented without any overflow or
truncation For example,
In FORTRAN:
26-55
Example 26.7
Notice that:
a) Bit 1 (the least
significant bit) is always
1.
b) Bit 2 is always 0.
c) Bit 3 alternates between
1 and 0, thus, it has a
cycle of length 2.
d) Bit 4 follows a cycle
(0110) of length 4.
e) Bit 5 follows a cycle
(11010010) of length 8.
2010 Raj Jain www.rajjain.com
26-56
26-57
Summary
26-58
Homework
26-59
Exercise 26.5
Implement the following LCG using Schrage's method
to avoid overflow:
26-64