Vous êtes sur la page 1sur 21

1

Topic 7

Random Number Generation

Contents
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
7.2 The Linear Congruential Method . . . . . . . . . . . . . . . . . . . . . . . . . . 5
7.3 Tests For Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
7.3.1 Frequency Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
7.3.2 Runs Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
7.3.3 Poker Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7.4 Random Variate Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7.4.1 Inverse Transform Method . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.4.2 Acceptance/Rejection Method . . . . . . . . . . . . . . . . . . . . . . . 10
7.5 Empirical Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2 TOPIC 7. RANDOM NUMBER GENERATION

7.1 Introduction
In this unit we talk about random numbers. In particular, we will address the following
questions:

What are random numbers?

How do we generate random numbers (for example for simulations)?

How do we test numbers for being random?

As a warm-up to this topic we look at a number of examples that claim to be random


numbers. Have a look at the following, and try to decide for yourself whether you would
call those sequences of numbers random or not:

 ;
01314 51333 6
  
 
      
 
 
  
    
   
        
      


 
   
 
  
  

       
 

      
   
          

c H ERIOT-WATT U NIVERSITY 2004


7.1. INTRODUCTION 3

 3141592653 5897932384 6264338327 9502884197 1693993751


0582097494 4592307816 4062862089 9862803482 5342117067
9821480865 1328230664 7093844609 5505822317 2535940812
8481117450 2841027019 3852110555 9644622948 9549303819
6442881097 5665933446 1284756482 3378678316 5271201909
1456485669 2346034861 0454326648 2133936072 6024914127
3724587006 6063155881 7488152092 0962829254 0917153643
6789259036 0011330530 5488204665 2138414695 1941511609
4330572703 6575959195 3092186117 3819326117 9310511854
8074462379 9627495673 5188575272 4891227938 1830119491
2983367336 2440656643 0860213949 4639522473 7190702179
8609437027 7053921717 6293176752 3846748184 6766940513
2000568127 1452635608 2778577134 2757789609 1736371787
2146844090 1224953430 1465495853 7105079227 9689258923
5420199561 1212902196 0864034418 1598136297 7477130996
0518707211 3499999983 7297804995 1059731732 8160963185
9502445945 5346908302 6425223082 5334468503 5261931188
1710100031 3783875288 6587533208 3814206171 7766914730
3598253490 4287554687 3115956286 3882353787 5937519577
8185778053 2171226806 6130019278 7661119590 9216420198
9380952572 0106548586 3278865936 1533818279 6823030195
2035301852 9689957736 2259941389 1249721775 2834791315
1557485724 2454150695 9508295331 1686172785 5889075098
3817546374 6493931925 5060400927 7016711390 0984882401
2858361603 5637076601 0471018194 2955596198 9467678374

Ideally, you have looked for patterns in these sets of numbers, or you might have thought
about how these numbers where created.
The data of the previous activity were generated as follows:

 0;
 my office phone number;
 random data from a normal distribution;
 random data from an exponential distribution;
 
the first 1250 digits of .

Let us first try to figure out what random means. Possible synonyms are typical,
representative, arbitrary, accidental, scrambled, aimless, patternless, senseless,
causeless, independent, incidental, indeterminate, indescribable, undirected,
uncontrolled, uncertain, unrelated, unordered, unpredictable.
An informal definition of randomness could be the following: Something is random if it is
not possible to describe it more briefly than to reproduce it. To be more precise we will
use the following working definition for random numbers:A (long) sequence of numbers
is random if the shortest computer program printing out the sequence has about the size
(length) of the sequence.
A formal definition of randomness for infinite sequences is the following:An infinite

c H ERIOT-WATT U NIVERSITY 2004


4 TOPIC 7. RANDOM NUMBER GENERATION

sequence of numbers is random if, for every block length  , the probability that a

particular block of length is observed is . !
For example for a sequence of numbers between 1 and 6 to be random it is not enough
that each number occurs with probability 1/6. We also require that each tuple of
numbers11, 12, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31,..., 66 occurs with probability
1/36, and that each triple of numbers like 111, 123, 461,... occurs with probability 1/216,
etc.
We first consider random numbers for the uniform distribution on the interval [0,1]. Recall
that for this distribution mean and variance are given by

# . 0 2* 143 )' # 0 and


"$#&%()+' *-, * /
5 # % ' * ,*76 " 1 # . 8 *-9 3 )' 6 : # 0
2 2
0

In practice it is hard to generate true random numbers, in particular on a computer.


Such numbers will always have patterns (in particular repetitions), and the aim is thus
to reduce such patterns.
Among others the following problems occur with computer generated ’random’ numbers:

; The numbers may not be uniformly distributed.


; The values are usually discrete, and not continuous.
; The (sample) mean may differ from 21 .
; There may be cyclic variations, including

– autocorrelation between numbers;


– numbers successively lower or higher than adjacent numbers;
– a couple of numbers below the mean followed by a couple of numbers above
the mean.

In practice we will have to apply tests to sequences of such numbers to check whether
they are sufficiently random or not.
There are also some more practical issues, even if we accept that we can only generate
pseudo-random numbers:

; The method (program) generating pseudo-random numbers should be fast.


; The pseudo-random numbers should have a long cycle. If 100,000 numbers are
needed for a simulation the cycle length should be at least that big.
; The random numbers should be replicable (yes, indeed (!)). This feature is needed
for debugging and comparison.
; The generated numbers should approximate the uniform distribution (or any other
distribution for which we generate random numbers).

<c H ERIOT-WATT U NIVERSITY 2004


7.2. THE LINEAR CONGRUENTIAL METHOD 5

7.2 The Linear Congruential Method


The linear congruential method is the most common method to produce pseudo-random
numbers on a computer. The method produces integers between 0 and according =?>$@
to the rule xn + 1 ACBEDGFIHKJML N
mod m, , , F-O D L =
and are given numbers.These numbers
are then used to represent the numbers y n AQF2HR =
in the unit interval [0,1]. The value
FO D L
is called the seed, the constant multiplier, and the increment. The choice of values
-F O D L = =
, , , has big impact on the quality of the random numbers. For common values
are 231 >S@ and 248 .
For = a power of 2 the period (cycle length) = can be achieved with LUA T 0 relatively
prime to = (i.e., the greatest common divisor gcd( L ,= ) = 1) and DWV 1 mod 4.
If LXA+Y then the largest possible period is = /4, which can be achieved with DZV 3 mod 8
or DWV 5 mod 8, and F[O odd.

Example
Problem:
We consider the values D\A @ ] , XL A+Y , = A_^ `aA&b c and F-O = 1, 2, 3, 4.
Solution: d
Y @ ^ ] c e b f g h @Y @@ @ ^ @ ] @ c @ e @ b
FOA @ @ @ ] c@ ^@ @f ^h e f ] f ] ] c e h e ] ch b @ ^e e @
F-OaA&^ ^ ^b @ g c^ ]c eg e
Y @Y ^
F-OaA ] ] ]h eh b ] e@ ^] c] cGf ] e f ^ f ] @ @ h e e @@ @ e ]
F-OaAic c e^ ] b ^
Y c
These numbers show how vital a good choice of the parameters is. Also note the large
gaps between the pseudo-random numbers. Even with the period length of 16 the gaps
between the random numbers are quite large.

7.3 Tests For Random Numbers


There are a number of tests that we can perform on sequences of pseudo-random
numbers to ensure that the sequences have the desired properties. Among them are
the following:
j Simple frequency test. This test uses the chi-square test to compare the (uniform)
distribution against the set of numbers in the sequence.
j Autocorrelation test.This tests for correlation between the numbers and compares
it to the expected correlation 0.
j Runs tests.These simple tests test for ups and downs, or for values above and
below the mean.
j Gap test.This test counts the number of digits that appear between repetitions of

kc H ERIOT-WATT U NIVERSITY 2004


6 TOPIC 7. RANDOM NUMBER GENERATION

a particular digit, and then uses the Kolmogorov-Smirnov test to compare with the
expected size of gaps.
l Poker test.Treats numbers grouped together as a poker hand. These hands are
compared to what is expected using the chi-square test.

We will treat some of these tests with the following set of data, created using the C++

mnpo q mnpo r mnsr


o mnut v
mnpxv mnpo r mnpwx mnpw q mnpy
m mnpzz
random number generator:

mnpvx mnpzo mnpwtmnpv


m
mnpz r mn{qt|mnpz r mnpv q mnsr
x mnpx r
mnut z mnpvw mnpvo mnpx
m
mnpxtmnpvx mnpz q mnpox mnpvo mnpxv
mnpx
m mn}m x mnpzx mnpyw
mnpvo mnpw q mnpy r mnsr m mnut z mn{qx
mnpx
m mnsrq mnpzz mnpz
m
mnpxo mnpww mnpzy mnpoy mnut v mnsrt
Our level of significance will always be ~€
mn}m y .
7.3.1 Frequency Test

n‚n‚n
The first test is a frequency test. We sub-divide the interval [0,1] into intervals S tm
of equal length (0,0.1), ,(0.9,1) and count the number of observed numbers in each ƒ…„
†
interval . The statistics ‡‰ˆŠŒ‹Q„u Ž ’‘”“-•E–I—2•™˜™š
has a chi-square distribution with 10 - 1
žŽ  y
degrees of freedom. Here ›œ„
is the expected number in each interval, which is —2• ›K„
Ž .

‡ ˆŽ Ÿ Ž  ž ¡ ¢  t vnpwt w
We test the null hypothesis that the data are from a uniform distribution. The hypothesis
is accepted if .

t z x q y v r o w tm
We find the following data

ƒ„ t q w o z x o y y y
xnpz mnpz xnpz t
npo t
npo mnpo t
npo m m m
‘‚“-•E–I—I—I• •™˜ š t znpo
and ‡  2 . Thus, the hypothesis is accepted.
This frequency test is usually applied if there is enough data so that each ›£„¥¤ y .
7.3.2 Runs Test
The first runs test is about ups and downs, i.e., whether the next number in the list
of random numbers is above or below the current number. The following sequence is
obtained from our data set.
+ - - + - + + - - + - + - + -
+ - + - - + - - + - + - + - -
- + + + - + - - + - + - - + -
+ - - +
¦ §
©ª¬«  ­¯® ¢ Ž – ˆ ¢
A consecutive sequence of ’s or ’s is called a run. If the sequence is truly random
the mean and variance of the number of runs are given by = 2N3- 1 and ¨
°
where is the total number of random numbers. If is sufficiently large this distribution °
of runs is approximately normal, and we can use standard hypothesis testing.
We test the null hypothesis that the data come from a uniform normal distribution at

±c H ERIOT-WATT U NIVERSITY 2004


7.4. RANDOM VARIATE GENERATION 7

significance level ²€³+´µ}´ ¶ .


We observed 37 runs, and calculate ·
³¹¸ º¼¾ »€½ ³¿¸ Á º¼Â »Àµp¶Ã¸à ¸ Z
³ Ä
µ ¸ ÃÃÃ
. For the two-tailed test the hypothesis is rejected if z < - z 0.05 or if z > z0.05 . The ÅÇÆ ÅÇÆ
ÅÇÆ
value for z0.05 is 1.960, and the null hypothesis that the data is from a uniform
distribution is accepted.
A different runs test tests for values above and below the mean. For this test we get the
following data
+ + + - + - + + + - + - + + +
- - - + - - + + - + - + - + -
- - - + + + + + - - - + - - +
- + - - +
If ÈÊÉ and È Æ are the numbers of observations below and above the mean (here Ë Ì and
ËÃ ), and Í the total number of runs (here ËÎ ), then Í is approximately normally distributed
with mean and standard deviation
ÓÚÙ
³½ ÆÇÏGÓ ÐÑÏÒ Ô ÆÉ and ¾ ³WÕ ÆÇÏ Ð Ï Ó Ò4Ò Ö ÆÇÖ ÓÏ Ð × Ï ÉÒØÙ ×
where Û is again the number of random numbers.
The distribution z = Ý Ü is approximately a standard normal distribution, and we find
b-

z = Þ É Æ ß Æá· àÇâ¯ã ³ZÄ


µ}´Ä ÃÄ
29 - 25.45
·
Since
» à ß à Éåä ÆKæ Ä
µ}´Ä ÃÄ æ à ß à Éåä Æ the null hypothesis is accepted.
7.3.3 Poker Test
We test for patterns in individual numbers. We generated random numbers of two digits
each.
If the numbers are truly random then Ä´´ % will have twice the same digit, while
10

Ä´´
100
90
% will have different digits.
îî22í í Ù Ò where ï é
100

We use again the chi-square test with random variable ç Æ ³¬èiéuê Éáë Æ Ö”ì-íE×I
are the observed frequencies, and ð é the expected frequencies.

ï é ð é ֔ì-íE×Iî2î2í Âí ٠Ò
With our data of 50 random numbers we find

Two different digits Ì º ̶ ´µ}´ ÎÂ


¸ ¶ ´µ

 
 Â
Two like digits
Since ç Æ ³+´µ Î æ ç à ß àáñ ë Æ ³&¶µpÎÎÄ the null hypothesis is accepted.

7.4 Random Variate Generation


Next we want to generate random numbers for more general probability distributions. We
will discuss two methods, the inverse transform method and the acceptance/rejection

òc H ERIOT-WATT U NIVERSITY 2004


8 TOPIC 7. RANDOM NUMBER GENERATION

method.

7.4.1 Inverse Transform Method


The following result gives in theory a method to generate random numbers for any
probability distribution:
ó
Let be a (continuous) random variable with cumulative distribution ôöõ÷ó[ø . Then the
random variable ùûú+ô7õ÷óüø
is a uniform random variable over the interval ý\þÿù þ .
Indeed, ù takes values between 0 and 1. Moreover, for every fixed ù ,7õ÷ù þÿ ù  ø‰
ú öõ÷ó$þ
ó  ø  úQô7õ÷ó  øÚú?ù   (as shown on the next graph) so that the cumulative distribution for ù
ù if ý þÿù þ
is
ý else

Figure 7.1
Taking
 the derivative of ô with respect to ù shows that the density function is indeed
ù ý\þÿù þ
if

ý else
This result yields the following algorithm for generating random numbers for a probability
distribution with density function  [õ÷ó[ø
and cumulative distribution ô7õ÷óüø ú

    : -õ÷óüø
 Draw uniformly random numbers  between 0 and 1.
 Substitute into  ú+ô7õ÷óø and solve for  .ó
In practice the last step is often difficult or even impossible and we have to look for
approximations of the function ó üú+ô õ Ñø

 .


c H ERIOT-WATT U NIVERSITY 2004
7.4. RANDOM VARIATE GENERATION 9

Examples

1.
Problem:
We look at the exponential distribution with density function

!"!#%$'&)(+0 *-,/. if "2143 and cumulative distribution (for x 1 0)


3 else
5 "#7698;: >?#A@>7698 : (+C @>76ED/FHG :
<= B & 0. I( C B 6MLNFOG +( *
. J
K .
Solution:
< 0
If we let P 6MLQFOG :R and solve for " then "S6TFVU ln WLQF P #
Thus, drawing uniformly random numbers P+X between 0 and 1 gives random numbers " X
then "Y6ZFHU ln WL[F PX # for the exponential probability distribution. Note that in practice
LQF P is replaced by P .
Figure 7.2 shows a frequency plot of 1000 random numbersP X drawn using the C++
random number generator. Here we used the inverse transform to get random numbers
for the exponential distribution. The interval length is 0.1.

Figure 7.2: Interval length 0.1

2.
Problem:
We look at the uniform distribution on the interval \ ]^)_)` . We generated uniformly random
numbers P on the unit interval \ 3 ^ L ` . To get random numbers for the uniform distribution
c <d if a e " ef_
on \ ]^)_)` recall that the density function for this distribution is a b 3 so that
else
5 =
the cumulative distribution is 0 if " eg] , 1 if "h1 _ , and "#76ij> e "#76M8 <= >?#W@k>l6
8 : c @>76 : c <d for a e x e b.
d <d b <d

m
c H ERIOT-WATT U NIVERSITY 2004
10 TOPIC 7. RANDOM NUMBER GENERATION

Solution:
So we solve n2oqp+t rs for u and get uvoxwzyM{}|H~Yw€n . Thus drawing random numbers
rs
n‚ for the uniform distribution on ƒ „†…‚‡‰ˆ and calculating uŠ‹oŒwyT{}|H~Yw€n‚ gives random
numbers for the uniform distribution on the interval ƒ w…)|)ˆ .

The above mentioned technique works well if the cumulative distribution has a closed
form and if we can calculate the inverse of this function, i.e., solve for u . However, for
many important distributions, notably the normal distribution, this is not possible. In
practice we have to approximate the function uŽoE r {nk . A simple and reasonably
good approximation for the inverse of the cumulative distribution of the standard normal
distribution is the function u‘o’ r {nk“o•”-–A— ˜š™/› Ÿ‰r  œ-r ”-žš–A— ˜š™/› . The approximation gives at
least one correct decimal of the inverse for n taking¢¡?values £W¤ between 0.00135 and 0.9986
(see Schmeiser, Approximations to the inverse cumulative normal function for use on
hand calculators, Applied Statistics 28:179-176, 1979).
Another (much better) approximation (Odeh, Evans, Algorithm AS 70: percentage points
of the normal distribution, Applied Statistics 23:96-97, 1974) is

u¥o‘¦§y©¨z«­ª ¬ –‚® «š¯ « where ¦Vo³² ~H´µ·¶;{W‡Q~¸nk and ¹  ,º  are the following values:
¨ ª«°¬ –± «š¯ «
¹ Ÿ oT~H„†»½¼I´I´I´I¼I´¾¿‡À„ÁIÁ º Ÿ  o „†»Â„ÃIÃI¼¾kÁ¾kÄI´+„Ä
¹  oT~Ň º  o„†»½ÆIÁIÁIÆIÁ†‡‚ÆÇ„+¾kÃIÆ
¹ȋoT~Ʉ†»½¼¾k´I´¾k´+„ÁIÁIƾÊÇ º‚È‹o„†»½ÆI¼†‡I‡À„¼¾kÄI´I¼IÄIÄ
¹ˋoT~Ʉ†»Â„´+„+¾k´I¼†‡‚´†‡À„´¾kÆ º‚ËÉo„†»·‡À„¼IÆI¼ÇIÇ+ÆI´IÁIÆ
¹̋oM~Ʉ†»Â„I„I„I„+¾kÆI¼Iľk´I´†‡À„¿‡Í¾kÁ ºÀÌÉo„†»Â„I„¼IÁIÆIÄ+„kÇ„I„ÄI¼¾
This approximation is accurate up to about 6 digits for „†»½ÆÏÎ ngÎ ‡ . Symmetry of
the normal distribution allows to extend this to the interval „ÐÎ nÑÎ 0.5 using the
transformation nÓÒÔ ‡‹~Õn and uSÒÔ ~Õu .

7.4.2 Acceptance/Rejection Method


Another method to generate random variate is the acceptance/rejection method. This
method is often used in practice.
Suppose we have a density function Ö{u! and we want to generate random data with
this density. Since Ö!{u! may be difficult to compute, or since it may be impossible to
compute the inverse of the cumulative distribution, we assume the existence of another
density function × which is easy to compute and which satisfies Ö{uHÎ9Ø» ×{u! for some
constant Ø (and all u ). The algorithm then works as follows:
Ù Generate Ú randomly for the uniform density on the interval {ۄ†…‚‡
Ù Generate Ü randomly for the density × .
Ù If ÚhÎYÖ!{Ü-Ý+ØÞ×{Ü¿ accept Ü , otherwise reject Ü .
Ù Repeat.

A sequence of numbers Ük accepted by this algorithm is a sequence of random numbers


for the density Ö!{u! .
The idea behind the algorithm is that a sequence of random numbers Ü for the density ×
is ’thinned’ to give a sequence of random numbers for the density Ö!{u! .

ß
c H ERIOT-WATT U NIVERSITY 2004
7.4. RANDOM VARIATE GENERATION 11

We note that the literature shows various modifications of this algorithm to increase
speed.

Examples

1.
Problem:
We consider the density function
çéèSê â â ì ë
à áâã7äæå ë 0 ì
í
else
where the graph is simply a straight line.

Figure 7.3: Density function of î


Solution:
í†ð)ëñ
ó'ô uniform density function on the interval ï
The has density function ò áâ!ã•ä
õ í ì â ì ë ë á â!ã
We do not have that î áâ!ã ìÏò áâã for all â , but we do have î áâ!ã ì ò
0 else
í†ð)ëñ
for all â . Thus we can use the uniform distribution on ï to generate random numbers
for the density ò as follows:
ö Generate ÷ randomly for the uniform density on á í†ð ã .
ê
ö Generate ø randomly for the density ò , which is the uniform density on ï í†ð)ëñ .
ö Accept ø if ÷ùìYî á ø ã)ú ë ò á ø ã , i.e., if ÷hì á õ ô ø ã-û;á ëýü õ ô ã7ä õ ô ø otherwise reject.
í†ð
The following is a list of 20 random numbers ÷Šþ uniformly distributed on the interval á ê ã ,
í†ð)ëñ
of 20 random numbers ø þ , uniformly distributed on the interval ï , and the verdict ’a’
or ’r’ for acceptance or rejection:

ÿ
c H ERIOT-WATT U NIVERSITY 2004
12 TOPIC 7. RANDOM NUMBER GENERATION

  
  ? a r
0.9501 0.9931 n r
0.2311 1.7995 y a
0.6068 1.6433 y a
0.4860 1.2898 y a
0.8913 1.6359 n r
0.7621 1.3205 n r
0.4565 0.6839 n r
0.0185 0.5795 y a
0.8214 0.6824 n r
0.4447 1.0682 y a
0.6154 1.4542 y a
0.7919 0.6189 n r
0.9218 1.1361 n r
0.7382 0.7408 n r
0.1763 1.4055 y a
0.4057 0.8898 y a
0.9355 1.3891 n r
0.9169 1.2426 n r
0.4103 1.0931 y a
0.8936 1.5896 n r
Thus, a sequence of random numbers for the density is
     
     
Of course, it would be much easier to use the inverse transform technique for this simple
example.

2.
Problem:
*
,+.- /4365   
+ 0- /  $21 $ 
Consider the density function !#"%$'&)(  which is plotted in
else
Figure 7.4.

7
c H ERIOT-WATT U NIVERSITY 2004
7.4. RANDOM VARIATE GENERATION 13

Figure 7.4: Density function of 8#9%:';


We want to generate random numbers for this distribution using the acceptance/rejection
method.
Solution:
The easiest way would be to use again the uniform distribution on < =>@?BA . However, with
this method we would reject many numbers. We will try to do better by using a piece-
wise linear function that approximates 8C9%:D; .
Figure 7.5 shows our density function plus the two lines given by the equations

EGF ?HJI
:
H ILKNM
EGF ?HPO Q9 ? K :';CR S
H I KNM HI T K M

U
c H ERIOT-WATT U NIVERSITY 2004
14 TOPIC 7. RANDOM NUMBER GENERATION

Figure 7.5: Density function and 2 lines


The first line is the tangent of V#W%X'Y through W[Z\]ZY , the second is the tangent through
W_^\@VCW_^6Y,Y with the constant term V#WQ`Yba d.egc fDh added. These lines were chosen since by
inspection both dominate the graph of VCW%X'Y in the interval i Z\@`Bj .
To simplify calculations we look at the two approximations
k al`nmZopoX
k aNqrZmpBstuXwv)^mupox

Figure 7.6: Graph of V and 2 lines


The two lines meet in the point Xzy|{}Zmuxs~p and k y{€^m^PuZs . Thus we look at the
piece-wise linear function

‚
c H ERIOT-WATT U NIVERSITY 2004
7.4. RANDOM VARIATE GENERATION 15

… … …“•”
ƒ2„%…'†ˆ‡ Š ‹‰ ŒnŽ if Ž’‘
…D“ ‘ … Ž –—˜~
- 0.7495x + 1.5736 if ‘ ‘NŒ
Ž else
ƒ„%…D†
This function will play the role of ™gš in the algorithm. Note that is not a density
ƒ„%…D†žƒ…Ÿ‡¡ 
function. Even though it is everywhere positive it does not satisfy ›ˆ œ
ƒ
Indeed, that integral is just the area under the piece-wise linear function œ , which is
¢ ¢¥¤P¦ ¢¥ª
œ ƒ2„%…'†žƒ…£‡ … ƒ…©¨
§ …G¨®  ƒ…
“ ŒnŽ P
¤ ¬
¦ « ŽB˜­– –—

œ ¤P¦ ª
‡ ŒnŽ …ª¯ 
Ž 
 B
 
˜ 
­ – …ª¯ ¨°  …²± ª
¯“ ¯ –— ¤P¦
¯ « ¯
Œ ¯ Œ ¯¤ ¦
‡ ŒnŽ …ª“ ŽB˜­– „ 2
… ª “ †C¨)  „ …“6†
« ˜ « 
 
– 
 
 — Œ «
”³  Œ Œ
ŒŽ˜Ž
„%…'†µ‡·¶ ƒ2„%…'† ‡
Call this value ´ (for area), then š ¸ is a density function and for ™ ´ we have
„%…D† „%…D†
that ™gš dominates ¹ .
Thus the algorithm becomes as follows:
º Draw » uniformly on „  6†
Ž¼ .
º Draw ½ with density š .
º If » ‘ ¹ „ ½ †]¾ ™gš „ ½ †²‡ ¹ „ ]† ¾ƒ2„ †
½ ½ then accept ½ , otherwise reject ½ .
To draw ½ with density š let us use the inverse transform method.
„%…D†ˆ‡ ¶ ƒ„%…D†
With š ¶,¿ ª _“ À]“ and the cumulative distribution
¢ ¤
Á „%…'†²‡ „%Â]†žƒÂ
š

œ
we have
  … … …2“
„%…D†ˆ‡ Š ‹‰ —­ŒŒif ŽG‘
… “ …‘
š - 0.6285x + 1.3070 if ‘ ‘NŒ
Ž else
depicted in Figure 7.7:

Ã
c H ERIOT-WATT U NIVERSITY 2004
16 TOPIC 7. RANDOM NUMBER GENERATION

1.4

1.2

0.8

0.6

0.4

0.2

0
-0.2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2

Figure 7.7: Densities Ä2Å%Æ'Ç and È#Å%Æ'Ç


For ÉGÅ%ÆDÇ we have that ÉGÅ%ÆDÇËÊ)Ì if Æ£ÍTÌ , and ÉGÅ%Æ'DzÊÏÎ if Æ£ÐNÑ . If ̒ÍÒÆÓÍNÑ we calculate

ÉÔÅ%Æ'ÇËÊÕ×Ö Ä2Å%Ú]ǞÛÚ
ØÙ
Ê Õ Ü Ö ÎÝÞßÑÑڈÛÚ
ÎÝÞßÑÑ
Ê Úžàá ÖÜ
Ñ á
á
ÊlÌÝâãÞÎäÆ à á
If Æ Ü ÍÒÆ£ÍNÑ then

ÉÔÅ%Æ'ÇËÊ Õ×Ö Ä2Å%Ú]ǞÛÚ


ØÙ
Ê Õ Ü ÖPå g(t) ÛÚzæ Õ Ö g(t)ÛÚ
ÖPå
Ê)ÌÝÌâãÝÞÞÑÎäâÆ2ã àÜèç ڞà á Ö ° æ ÎÝéÌêÌÚë
Ñ á Ö
á Öå
á
Ê ç ÌÝéÎ6êBÆ à æ)ÎÝéÌêÌÆ ç Ö å ÌÝ éÞâÎ
Thus
Ì if ÆÓÍTÌ
î 0.8461x2 if 0 ÍÒÆðÍÒÆ Ü
ÉGÅ%ÆDÇˆÊ ìíí ç
ÌÝéÎäãÑÆ à æ)ÎÝéÌêÌÆ ç ÌÝéÞâÎ if Æ Ü ÍÒÆÓÍNÑ
1 else
ííï
The graphs of the density Ä and its cumulative distribution É are pictured in the graph
below. The gap in the graph stems from the fact that we made several rounding errors.

ñ
c H ERIOT-WATT U NIVERSITY 2004
7.4. RANDOM VARIATE GENERATION 17

Figure 7.8: Graph of ò and its cumulative distribution ó


With ôBõ•ö÷óÔø%ùõPúˆö)ûüýþÿ the inverse ó6ø[ô~ú is the function given by
  
õ
  if  û TôTôBõ
ó  ø%ùDúˆö 1.3070 -  1.30702   õ
  
 õ
 


 õ
  if 6ô õTô 

where the second part simplifies to
, õ_! õ  $ $ 
 #" õ
  
Note again that the calculations are not entirely accurate due to rounding errors.
Now we can finally write down the algorithm to draw random numbers for the density % :
& Draw ' uniformly on ( û*)+
, .
& Draw ô uniformly on ( û*)+
, .
& If ûT   ,õ!_õ  $ $ 
 ô-T  ûüýþÿ let . be õ
 , else .Gö   "
õ    .

& If '0/1C% ø24
. ú35
4 ø24
. ú accept . , otherwise reject.

6
c H ERIOT-WATT U NIVERSITY 2004
18 TOPIC 7. RANDOM NUMBER GENERATION

7.5 Empirical Distributions


We treat empirical distributions by way of example.

Example
Problem:
Suppose that data is collected about repair times (in hours):

Frequency Rel.Freq Cum.Freq


07 x7 0.5 31 0.31 0.31
0.5 7 x 7 1.0 10 0.1 0.41
1.0 7 x 7 1.5 25 0.25 0.66
1.5 7 x 7 2 34 0.34 1
The true cumulative probability function is unknown to us.

Figure 7.9: Cumulative distribution


Solution:
The inverse transform technique applies here directly. If for example : 8 9 , a random
number from the uniform distribution, is 0.83, then we can read off the ; 9 value from
the graph as 1.75 as in Figure 7.10.

<
c H ERIOT-WATT U NIVERSITY 2004
7.5. EMPIRICAL DISTRIBUTIONS 19

Figure 7.10
If we need more random numbers we will use a more systematic approach.
If =0>@?A>@=*BDC the cumulative probability function is EGFIH
J K
LMNH ?PFQ=*BDRS5? or ?TFVUWBDR*UE .
Here E takes values between 0 (?XFY= ) and 0.31 (?ZF[=*BDC ).H
J O
MNH
If =*BDC1>\?]>^U the cumulative function has slope H
J _LMNH
J K
L F`=*BDS and the intercept is
LJ H
MNthe
found by solving the equation =*BbacUdF]=*BDSdefUWBg=ihkj using H
J O fact that the point l UWBg=*m=*BbacUon
is on the line. Thus, the intercept is =*BDS*U and the cumulative function has the equation
EFY=*BDS5?hk=*BDS*U and E takes values between 0.31 and 0.41. The inverse is the function
U
?XF l2Eqpr=*BDS*Uon
= BDS
*
FiC5EsptUWBg=uC

In general, if we have a line through l2? mE n and l2? mE n then the slope of the line is y vw M vy x
H H L L y wyM x
and the intercept is found by solving, for example, E F\y vw M yvx ? hzj so that j{F w|y vx M y xvw
H wM x H wM x
This way we get the following table describing the inverse of the cumulative distribution:
=*Bg==->}E~>t=*BD*U ? F WU BDR*UE
=*BD*Ud>}E~>t=*BbacU ? F C5Eqp1UWBg=uC
=*BbacUd>}E~>t=*BDRR ? F S5EihA=*B€U+
=*BDRR>}E~> UWBg== ? F UWBbaf‚ƒEdhk=*BDC

„
c H ERIOT-WATT U NIVERSITY 2004
20 TOPIC 7. RANDOM NUMBER GENERATION

Figure 7.11
This function can now be used to generate random numbers for our empirical distribution
using the inverse transform method.

7.6 Summary
… Complex systems are often simulated using computers. For this we need random
numbers distributed according to various distributions.
… One basic problem arises that random numbers generated by a computer are
only pseudo-random. Nevertheless we have to design methods to generate such
numbers which come close to being random.
… In practice it is enough to implement a random number generator for the uniform
distribution on the unit interval † ‡*ˆ+‰
Š . Usually the linear congruential method is
used. Random numbers for other distributions can be generated using the inverse
transform technique. This technique also applies to empirical distributions.
… In practice the acceptance/rejection method is often used to generate random
numbers for density functions where the inverse transform method does not apply.
… Tests are employed to check whether sequences of numbers can be considered
random or not.

‹
c H ERIOT-WATT U NIVERSITY 2004
GLOSSARY 21

Glossary
acceptance/rejection method
A method to generate random numbers for a density Πby using random numbers
from another density  such that Œ~Ž‘ for some constant  . Some of the random
numbers for  are accepted to become random numbers for Œ , some are rejected.

frequency test
The frequency test uses the chi-square distribution to test how the frequency
distribution of a sequence of pseudo-random numbers compares to random
numbers of the uniform distribution.

linear congruential method


The linear congruential method is the most common method to produce pseudo-
random numbers on a computer. The method produces integers between 0 and
’\“•” according to the rule x ’ are given
n + 1 –˜—š™f›Nœ +ž where › Ÿ , ™ , ¡ ,  and
numbers.

poker test
The poker test for sequence of pseudo random numbers treats numbers grouped
together as a poker hand. These hands are compared to what is expected using
the chi-square test.

pseudo random number


Pseudo random numbers are sequences of numbers generated by a device (often
a computer) that are supposed to model truly random numbers.

random numbers
An infinite sequence of numbers is random if, for every block length ¢ , the
probability that a particular block of length ¢ is observed is ”o£ ¢ .

runs test
The simple runs tests test for ups and downs, or for values above and below the
mean in a sequence of pseudo-random numbers.

seed
The seed is the starting value in a method to generate random numbers.

¤
c H ERIOT-WATT U NIVERSITY 2004

Vous aimerez peut-être aussi