Professional Documents
Culture Documents
http://ebooks.cambridge.org/
Chapter
6 - Probability and Random Numbers pp. 75-87
Chapter DOI: http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge University Press
6
Probability and Random Numbers
rand
/0 Generates a random number in (0,1)
randOseed' ,13) /0 Set the 'seed' of algorithm to 13
75
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:38 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
76
bl = rand(25,1)
rand('seed' ,0)
A = rand(3,4)
rand ('seed' ,13)
b2 = rand(25,1)
/0
/0
/0
/0
/0
and this vector represents time=[Year Month Day Hour Min Sec] so
we could use
to link to the nearest second, though this doesn't always seem to work as
well as setting the seed yourself, preferably to an odd number. Numbers
in the range 10000-20000 (or actually the default 0) are good choices.
The simple MATLAB function rand generates a column of n random
numbers uniformly distributed in (0,1) by x=rand(n,l), or a row by
x=rand(l,n).
Similarly, rand(n) generates a n x n random matrix
while rand(m,n) produces a m x n matrix with entries in (0,1).
To generate random numbers in any interval (a, 6), use a linear transformation (x > y), for example
x = rand(l,30);
a = 12; b = 99;
y = a + (b-a) * x;
Notice that the elements of vector y are now within (a, b).
ceil(3)=3,
fix(2)=2,
ceil(1.9)=2,
fix(-1.6)=-l,
ceil(1.5)=2,
fix(-2)=-2,
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:38 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
11
f l o o r ( 1 . 2 ) = l , floor(3)=3, f l o o r ( l . 9 ) = 1 ,
floor(1.5)=l,
round(1.2)=1, round(3)=3, round(1.9)=2,round(-1.5)=-2.
We can get a column of n numbers taking integer values 0 , 1 , . . . , k
(that is, integer values in the closed interval [0, k]) with equal probability
using
x = rand(n,l);
floor((k+l)*x)
ceil(k*x)
round(10*x)
r = randperm(9)
A = c e i l ( rand(9)*5 )
B = A(r,:)
rand('seed ; , 0 ) ; x=rand(100,l);
c=(x>l/3)
/0 Note x in (0,1)
The second line here instructs the computer to print 0 if x 'fails' the test
x > ^ and 1 if x 'passes'. Assuming that x has a uniform distribution
in [0,1] you should get 0s in approximately | of the cases. You can test
this by typing sum(c)/100 and seeing whether the answer is close to | .
However, a more challenging method than c=(x>l/3) is the following
/o Note x in (0,1)
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:38 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
78
x = rand(2 ,6)
% Case (1) with [1 ,4]
% Case (2) with [0 ,3]
e the results:
y =
z =
1
3
0
2
2
3
1
2
2
4
1
3
1
2
0
1
3
1
2
0
2
1
1
0
/.
; /0
/0
/0
6
Set 'seed' to 19
Random in (0.5, 6.499)
Random numbers in [1,6]
Count faces in x
randn('seed J ,12);
x=randn(1000);
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:38 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
79
where seed is similar to that used for rand, but is specific to randn!
Using again a linear transformation, to obtain numbers with mean m
and standard deviation s, type
y = m + s*x;
randnOseed',11)
x = randn(2000,2);
y = 3 + 5*x;
hist(y)
/0
/0
/0
/o
Set 'seed' to 11
N(0,l) of mean=0 & Std=l
Transformation
Plot the simulated data y
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:38 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
80
number generator rand. To simulate a negative exponential distribution, with both mean and standard deviation being , we can use the
following relation
assuming that x is from a uniform distribution U(0,l). For your convenience we have developed an M-file exprand.m, generating this distribution.
To illustrate, we can simulate and compare samples of n = 20000
numbers of
a uniform distribution in (0, 2.4) with mean m = 1.2;
a normal distribution with mean m 1.2 and standard deviation
s = m = 1.2;
an exponential distribution with mean I 1.2:
rand('seed',0); n=20000;
m=1.2; s=1.2;
x = unirand(0,2.4,n,l);
randn('seed',0);
y = normrand(m,s,n,l);
randCseed',0);
z = exprand(m, n , l ) ;
subplot(131) ; hist(x,90)
subplot(132); hist(y,20)
subplot(133); hist(z,40)
This example is available in the M-file c6exp.m. The results can be seen
in Figure 6.1, where one graph shows an exponential distribution! The
mean and standard deviation of a negative exponential distribution are
both vn and so you may use MATLAB commands mean and std to
verify that, for z, both quantities are close.
As discussed in 6.6, in practice, inter-arrival times satisfy the negative
exponential distribution. For instance, take the example of five arrival
times (occurrences) of an event as arr = [1.3 2.4 5.1 6 8.3]. Then interarrival times are the time differences between arrival times (occurrences),
that is, int_arr = [1.3 1.1 2.5 0.9 2.3] counting from t = 0.
So we can simulate inter-arrival times by exprand.m but how do we
work out arrival times then? Each arrival time is a cumulative sum of
inter-arrival times; for the above example, given int_arr, we work out
arr by
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:38 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
81
200
100
0.5
1.5
4000
2000
10
=
=
=
=
int_arr(l) = 1.3;
int_arr(l) + int_arr(2) = 2 . 4 ;
int_arr(l) + int_arr(2) + int_arr(3) = 5 . 1 ;
sum of c i n t _ a r r ( l ) + . . . + i n t _ a r r ( 4 ) ' = 6;
a r r ( 5 ) = sum of
int_arr(l) + . . . +int_arr(5); = 8 . 3 .
tk = ^2zi,
fc
= 1,2,. - , 20000.
2=1
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:39 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
82
m = 180 / 100;
z = exprand(m,50,1);
rand('seed',0);
hist(z)
t=cumsum(z)
6.1
Exercises
An electrical retailer estimates that, in the weeks before Christmas, there are, on average, 140 customers per day for electronic
games, with inter-arrival times satisfying a negative exponential
distribution. (A shop day is 9 am-5 pm.) On average | pay cash
and the rest use a credit card, 50% of the sales are for Nintendo
(Super Mario) and 50% are for Sega (Sonic the Hedgehog). You
are asked to simulate (the first) 30 sales from the opening time,
assuming that each customer buys a product.
(a) Simulate the inter-arrival and arrival times for the first
30 customers of the day. Find the mean and standard
deviation for the inter-arrival times.
(b) Print out a table simulating the first 30 sales in the form
of Arrival Time, in minutes with 1 decimal place, Payment Method (Cash as +1 and Credit as 1) and Product
Sold (Nintendo as +1 and Sega as 1)
Hints:
Your answer to question (b) will be three columns (all
of size 30):
one set of arrival times,
one set to select cash or credit card and
one set to select Nintendo or Sega.
Consult 6.2 to see how to obtain numbers which are
0, 1 with probability | , | , or both with probability ^.
How do you then convert such a vector of 0s and Is
into - I s and +ls ?
MATLAB cannot print different columns of the same
matrix in different formats; they all need to be integers
or all decimals. To change this you need to use the
command fprintf or sprintf. For example, try
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:39 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
Exercises
83
a = p i , b = 35, c = 15
c_l = [ a b c ]
iprintf('c_2 = /05.3f 705d "yUAn',a,b,c)
c_3 = sprintf (>0/05.3f /e5d / o d\n\a,b,c)
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:39 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
84
6.6 Appendix
6.6.1 Generation of random numbers
The popular approach of generating random numbers is by the so-called
multiplicative congruential method. It is implemented in the MATLAB
command rand (' seed y , j) where j is an integer as discussed before
and is called the MATLAB 4 generator. Another approach of generating random numbers is based on scaled Fibonacci numbers with a
shift (see Chapter 3). This is implemented in the MATLAB command
rand ( ' s t a t e ' , j ) where j is an integer and is called the MATLAB 5
generator. For example with the default j = 0,
'/.'/. gives
*/.'/. gives
rand = 0.9501
rand = 0.2190
i = 1,2,3,...,
where lMod M means that we subtract as many multiples of M as possible, and take the remainder as our answer. For any i, Ui is a random
number such that 0 < Ui < M. Therefore U{ corresponds to a random
number r{ = Ui/M G [0,1). To use this method, once suitable constants
jRT, M (usually integers) are chosen, we only need to supply an initial
value u\ which is usually called a 'seed'! Usually K is of about the same
magnitude as the square root of M but M, although large, must not be
a multiple of K.
With MATLAB, the method for one-step is simply implemented as
rand_num = unew / M
for j = l:n
>>
rand_num = seed / M
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:39 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
6.6 Appendix
85
does! For example we have used a=randme (4) with seed=2 and shif t=0,
taking K = 32 and M = 5, to produce the following 'home-made' four
random numbers: rand = 0 . 8 , 0 . 6 , 0 . 2 , 0 . 4 .
To find out what the current seed is, type s = r a n d ( J s e e d 7 ) , while
r a n d C ' s e e d ' ,45) is to change the seed to 45. Therefore, to observe a
sequence of seed changes in MATLAB type
>>
>>
s = rand('seed') ;
fprintf('MATLAB Seed
for j = 1:5, r=rand;
fprintfOMATLAB Seed
The distribution of the pseudorandom numbers is continuous (up to machine precision) and uniform (equally likely in the range (0,1)). For any
distribution of random numbers, the probability of sampling a particular
value is governed by the probability density function (pdf) / so that if
we denote by X the random variable, then for any values x\ and x2
such that xi < x2i it follows that
rx2
f(t)dt.
F(x) = /
/0O
f{t)dt
Joo
with
f(t)dt = 1.
Joo
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:39 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
86
otherwise,
0,
x < 0,
x, z [ 0 , l ) ,
1,
x>l.
Normal distributions The distribution of a normal random variable
X is governed by the pdf
or equivalently the cdf
exp {This distribution has the mean /x and standard deviation a, denoted by
When ii = 0 and cr = 1, N(0,l) is the so-called standard normal
distribution which is simulated by randn in MATLAB. The distribution
N(/i, <72) can be obtained by the transformation z ax + /i, if x is the
standard normal variable N(0,l).
Negative exponential distributions A negative exponential distribution describes a Poisson process. Recall that a series of events (denoted
by random variable X) is a Poisson process if
the number of outcomes in each period is independent;
the probability of more than one event in such a small interval is
negligible;
the probability of a single event during a (very short) interval is proportional to the length of the interval, that is,
lim
/i->o
otherwise,
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:40 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016
A,
6.6 Appendix
87
[K*\-e-,
0,
z>0,
otherwise.
As the cdf is in a simple and closed form, we can link it with the uniform distribution (another simple cdf) and thus do not need a separate
random number generator. If x is selected from a uniform random distribution, to generate an exponential distribution z, let
1 - e~Xz
= x
giving ( = A" 1 )
z = ^log(l x).
This is actually what M-file exprand.m does. For the bank example in
6.5, where there are 131 customers (arrivals) in 180 minutes, the average
inter-arrival time is = ho = J | J minutes. Therefore, the probability of
an arrival (event) in any short interval (, t + ho) is V = 1 so
V{{tt
h^o
+ h)}
V{(tt
h)}
ho
Cambridge
Books Online Cambridge University Press, 2010
Downloaded from Cambridge Books Online by IP 81.180.218.225 on Fri Apr 22 09:38:40 BST 2016.
http://dx.doi.org/10.1017/CBO9780511624117.007
Cambridge Books Online Cambridge University Press, 2016