# Random Numbers and Random Variates

Document Sample

```					                      Section VI.2

Random Numbers and
Random Variates

Part 1
Generation of Random Variable

CS457                                                      347

Discrete Event Simulation
Input parameters such as interarrival time and
service time are often modeled by random
variables with some given distributions
A mechanism is needed to generate variates for a
wide class of distributions
this can be done if a sequence of random numbers that
are independent of each other and uniformly
distributed between 0 and 1, is available.

CS457                                                      348
Random Numbers
Uniformly distributed between 0 and 1
consider a sequence of random numbers u1,u2,…,uN
n equal
sub-intervals
•••
0                                        1
uniformity: expected number of random numbers in
each sub-interval is N/n
independence: value of each random number is not
affected by any other numbers
map onto F(x) axis would yield the required
probability
CS457                                                      349

Inverse Transformation Method –
Discrete Distributions
Example: P(1) = 0.5, P(2) = 0.3, P(3) = 0.2
Cumulative distribution function, F(x)
1.0
0.8

F(x)   0.5

0      1     2   3
x
CS457                                                      350
Inverse Transformation Method –
Discrete Distributions
Algorithm
generate random number u
variate X = i if F(i - 1) < u < F(i)

Example: F(0) = 0, F(1) = 0.5, F(2) = 0.8, F(3) = 1.0
0 < u < 0.5            variate X = 1
0.5 < u < 0.8          variate X = 2
0.8 < u < 1.0          variate X = 3

CS457                                                      351

Common Discrete Distributions
Bernoulli trial (with parameter p)
P(1) = p,            P(0) = q = 1 – p
x = p,               σ2 = pq

Random variate generation
generate u
If 0 < u < 1 – p, X = 0; otherwise X = 1

CS457                                                      352
Common Discrete Distributions
Discrete uniform (with parameters a and b)
P(n) = 1/(b – a + 1) for n = a, a + 1, …, b
x = (a + b)/2           σ2 = (b – a)(b – a + 2)/12

Random variate generation
generate u
X = a + (b – a + 1)u

CS457                                                    353

Common Discrete Distributions
Binomial (with parameters p and t)
P(n) = pnqt-n where q = 1 – p and n = 0, 1, …, t
x = tp                 σ2 = tpq

Random variate generation
generate t Bernoulli variates, y1, y2, …, yt
Binomial variate X = y1 + y2 + … + yt

CS457                                                    354
Common Discrete Distributions
Poisson (with parameter λ)
P(n) = λ ne-λ / n! for n = 0, 1, …
x=λ                      σ2 = λ

Random variate generation (based on the
relationship with exponential distribution)
let b = 1, i = 0
Repeat until variate X is found
– generate u
– b=b .u
– If b < e−λ, then X = i; otherwise i = i + 1
CS457                                                            355

Common Discrete Distributions
Geometric (with parameter p)
P(n) = pqn for n = 0, 1, …,          where q = 1 - p
x = q/p,          σ2 = q/p2
can be interpreted as the probability of n failures in
successive Bernoulli trials before a success occurs
has memoryless property

Random variate generation
generate u
X = ln(1 – u)/ln(1 – p)
CS457                                                            356
Inverse Transformation Method –
Continuous Distributions
Algorithm
generate random number u
solve F(x) = u for variate X

1

F(x)       u

0
Variate X
x
CS457                                                       357

Common Continuous Distributions
Uniform (with parameters a and b)
1/(b − a)      a ≤ x ≤ b,
f ( x) =
0        otherwise.

F(x) = (x – a)/(b – a)                       a<x<b
x = (a + b)/2                          σ = (b – a)2/12
2

Random variate generation
generate u
X = a + (b – a)u
CS457                                                       358
Common Continuous Distributions
Exponential (with parameter µ)
f (x) = µe-µx
F(x) = 1 - e-µx
x = 1/µ,            σ2 = 1/µ2

Random variate generation
generate u
X = – (1/µ )ln(1 – u)

Can also use X = – (1/µ )ln(u)
CS457                                              359

Common Continuous Distributions
Erlang (with parameters µ and α)
f (x) = (µαxα−1e-µx)/(α – 1)!
j −µ x
F(x) = 1 – α=0 ( µ x ) e / j !
j
−1

x = α /µ,          σ2 = α /µ2

Random variate generation (based on the
relationship with exponential distribution)
generate α random numbers, u1, u2,… ,uα
X = −(1/ µ ) ln ∏α=1 µi
i

CS457                                              360
Common Continuous Distributions
Hypoexponential (with parameters p, µ1, and µ2)
f ( x) = pµ1e − µ1x + (1 − p )µ 2e − µ 2 x
F ( x ) = 1 − pe− µ1x − (1 − p)e − µ 2 x
x = p / µ1 + (1 − p) / µ 2                     σ 2 = 2 p / µ12 + 2(1 − p) / µ 2 − x 2
2

Random variate generation
generate u
If u < p, variate X = exponential variate with
parameter µ1
else variate X = exponential variate with parameter µ2
CS457                                                                                      361

Common Continuous Distributions
Normal (with parameters µ and σ)
1                    2 / 2σ 2
f ( x) =            e − ( x− µ )
2πσ 2
x = µ , variance = σ 2

Random variate generation
approximation Method
– generate two random numbers u1 and u2
– X 1 = µ + σ −2 ln u1 cos(2π u2 ) and
X 2 = µ + σ −2 ln u1 sin(2π u2 ) are random variates
CS457                                                                                      362
Empirical Distribution
Could be used if no theoretical distributions fit the
Example - piece-wise linear empirical distribution
organize x-axis into K intervals
suppose interval k is from ak-1 to ak and relative
frequency of interval k is P(k), k = 1, 2, … , K

CS457                                                          363

Empirical Distribution
Random variate generation
generate u
use method for discrete distribution to select one of the
K intervals
let the selected interval be j, compute variate X as
follows
X = aj-1 + w(aj – aj-1)/P(j)
−
where w = u – ij=11 P(i)

CS457                                                          364
Part 2
Random Number Generators

CS457                                                         365

Random Number Generators (RNG’s)
Requirements
sequence generated has uniform distribution
(continuous) between 0 and 1
the numbers in the sequence are independent of each
other
RNG’s in computer simulation are pseudorandom
each number in the sequence is determined by one or
several of its predecessors
statistical tests can be used to determine how well the
requirement of uniformity and independence are met

CS457                                                         366
Desirable Properties
Uniformity and independence
Should be able to reproduce a given sequence of
random numbers
help program debugging
helpful when comparing alternative system design
Should have provision to generate several streams
of random numbers
Computationally efficient

CS457                                                                  367

Linear Congruential Generator (LCG)
Commonly used algorithm
A sequence of integers Z1,Z2,… between 0 and m-
1 is generated according to
Zi = (aZi-1 + c) mod m
where a and c are constants, m is the modulus and Z0 is the
seed (or starting value)
Random numbers u1,u2,… are given by ui = Zi/m
Recursive relationship - sequence can be
reproduced if the seed is known

CS457                                                                  368
Linear Congruential Generator (LCG)
Example
a = 7, c = 3, m = 10, seed = 3
sequence: 3, 4, 1, 0, 3, 4, 1, …

Example
a = 4, c = 2, m = 9, seed = 3
sequence: 3, 5, 4, 0, 2, 1, 6, 8, 7, 3, 5, 4, …

CS457                                                       369

Properties of LCG
Can have at most m distinct integers in the
sequence
as soon as any number in the sequence is repeated, the
whole sequence is repeated
period: number of distinct integers generated before
repetition occurs
The ui’s can take on values 0, 1/m, 2/m,…,
(m-1)/m only
m should be selected to be very large in order to
achieve the effect of a continuous distribution
(typically, m > 109)
CS457                                                       370
Mixed LCG
For mixed LCG, c > 0
Full period is achieved if
c is relatively prime to m
if 4 divides m, then 4 divides a - 1
for any prime number q that divides m, q also divides
a-1
example: a = 4, c = 2, m = 9

CS457                                                          371

Mixed LCG
Choice of a, c, m
for computational efficiency, a good choice for
m = 2b
when m = 2b, conditions for full period becomes
– c is odd
– a - 1 is divisible by 4
note: full period may be a nice property; uniformity
and independence are more important

CS457                                                          372
Multiplicative LCG
For multiplicative LCG, c = 0
Generally perform as well as mixed LNG
Good choice for m is largest prime < 2b
example: for b = 31, 231 - 1 is prime

CS457                                                         373

Test for Uniformity and Independence
Can use goodness-of-fit test
Example: χ2 test
sample of n observations
null hypothesis H0: sample is from population with
some specified distribution
select k intervals into which the observations fall
f0(j) = number of observations in the interval j

CS457                                                         374
Goodness-of-Fit Test
χ2 test (cont.)
fe(j) = expected number of observations in interval j
(determined by the specified distribution)
compute χ = kj =1[ f0 ( j ) − fe ( j )] / f e ( j )
2                          2

reject H0 at level α if χ > χ k −1,1−α
2      2

CS457                                                           375

Test for Uniformity
Sample of n random numbers: u1,u2,…,un
H0: sample is from population with uniform
distribution (0,1)
Divide [0,1] into k intervals of equal length
For any interval j, fe(j) = n/k
2
n       n
χ = j =1 f0 ( j ) −
2     k
/
k       k
Reject hypothesis of uniformity at level α if
χ 2 > χ k −1,1−α
2

CS457                                                           376
Test for Independence
Approximate test
Example: serial test
sample: n pairs of random numbers
(u1, u2), (u3, u4), …, (u2n-1, u2n)
k2 intervals: each corresponds to a square
1 2           •••      k
1
2
•
•
•
k
CS457                                                                   377

Test for Independence
Serial test (cont.)
if the sequence is uniform and u2i-1 and u2i are independent, then
Pr[jth square is selected] = 1/k2, j = 1,2,…,1/k2
for any interval j, fe(j) = n/k2
2
k2                  n            n
χ2 =    j =1   f0 ( j ) −            /
k2           k2
evidence against independence if we reject null hypothesis
note: only test independence of two adjacent random numbers
can extend to more than two dimensions

CS457                                                                   378

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 18 posted: 6/1/2010 language: English pages: 16