(New page: 1. Generating uniformly distributed random numbers between 0 and 1: U(0,1) a) Using matlab functions The rand function returns uniformly distributed random numbers between 0 and 1. r = ...) |
|||
Line 1: | Line 1: | ||
− | + | == Generating uniformly distributed random numbers between 0 and 1: U(0,1) == | |
− | a) | + | === a) Linear Congruential Generators === |
+ | A linear congruential generator has the property that numbers are generated according to | ||
− | The | + | Zi = (aZ(i-1) + c) mod m |
+ | |||
+ | where m, a, c and Z0 are non-negative integers. We say that m is the modulus and that Z0 is the seed. The sequence that we obtain clearly satisfies 0<= Zi < m. In order to generate pseudo-random numbers, U1,...,Un,..., we set Ui = Zi/m. Note that Ui is inside(0,1) for each i. | ||
+ | |||
+ | We now act as though the Ui's constitute an independent sequence of U(0,1) random variables. | ||
+ | |||
+ | |||
+ | === b) Using MATLAB Functions === | ||
+ | |||
+ | rand(n) | ||
r = rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1). r = rand(m,n) or r = rand([m,n]) returns an m-by-n matrix. | r = rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1). r = rand(m,n) or r = rand([m,n]) returns an m-by-n matrix. | ||
Line 11: | Line 21: | ||
ans = 0.8147 0.9058 0.127 0.9134 0.6324 | ans = 0.8147 0.9058 0.127 0.9134 0.6324 | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == Generating Normal Random variables == | ||
+ | |||
+ | ===a) Box-Muler method=== | ||
+ | |||
+ | If Z- N(0, I2) then | ||
+ | |||
+ | R=(Z1)^2 + (Z2)^2 is exponentially distributed with mean 2. | ||
+ | |||
+ | Given R, the point(Z1, Z2) is uniformly distributed on the circle of radius sqrt(R) centered at the origin. | ||
+ | |||
+ | Generate R and choose a point uniformly from the circle of radius sqrt(R). (r = -2log(U1)) | ||
+ | |||
+ | To generate a random point on a circle, generate a random angle uniformly between 0 and 2pi and map the angle to a point on the circle. | ||
+ | |||
+ | The random angle can be generated as V=2*pi*U2 and the corresponding point has coordinates (sqrt(R)*cos(V)), (sqrt(R)*sin(V)). | ||
+ | |||
+ | === b) Using MATLAB Functions === | ||
+ | |||
+ | normrnd(mu, sigma) | ||
+ | |||
+ | r=normrnd(mu, sigma) generates random numbers from the normal distribution with mean parameter mu and standard deviation parameter sigma. mu and sigma can be vectors, matrices, or multidimensional arrays that have the same size, which is also the size of R. A scalar input for mu or sigma is expanded to a constant array with the same dimensions as the other input. | ||
+ | |||
+ | |||
+ | [[References:]] | ||
+ | |||
+ | Martin Haugh, " Generating Random Variables and Stochastic Process", 2004 | ||
+ | [http://www.columbia.edu/~mh2078/MCS04/MCS_generate_rv.pdf] | ||
+ | |||
+ | MathWorks | ||
+ | [http://www.mathworks.com/index.html] | ||
+ | |||
+ | Ozgur KARAYALCIN, "Generating Random numbers and Random variables", 2007 | ||
+ | [http://www.mathematik.uni-kl.de/~seifried/eng/teaching/teach_07ws/ozgur.pdf] |
Revision as of 15:31, 15 April 2013
Contents
Generating uniformly distributed random numbers between 0 and 1: U(0,1)
a) Linear Congruential Generators
A linear congruential generator has the property that numbers are generated according to
Zi = (aZ(i-1) + c) mod m
where m, a, c and Z0 are non-negative integers. We say that m is the modulus and that Z0 is the seed. The sequence that we obtain clearly satisfies 0<= Zi < m. In order to generate pseudo-random numbers, U1,...,Un,..., we set Ui = Zi/m. Note that Ui is inside(0,1) for each i.
We now act as though the Ui's constitute an independent sequence of U(0,1) random variables.
b) Using MATLAB Functions
rand(n)
r = rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1). r = rand(m,n) or r = rand([m,n]) returns an m-by-n matrix.
Example: rand(1, 5)
ans = 0.8147 0.9058 0.127 0.9134 0.6324
Generating Normal Random variables
a) Box-Muler method
If Z- N(0, I2) then
R=(Z1)^2 + (Z2)^2 is exponentially distributed with mean 2.
Given R, the point(Z1, Z2) is uniformly distributed on the circle of radius sqrt(R) centered at the origin.
Generate R and choose a point uniformly from the circle of radius sqrt(R). (r = -2log(U1))
To generate a random point on a circle, generate a random angle uniformly between 0 and 2pi and map the angle to a point on the circle.
The random angle can be generated as V=2*pi*U2 and the corresponding point has coordinates (sqrt(R)*cos(V)), (sqrt(R)*sin(V)).
b) Using MATLAB Functions
normrnd(mu, sigma)
r=normrnd(mu, sigma) generates random numbers from the normal distribution with mean parameter mu and standard deviation parameter sigma. mu and sigma can be vectors, matrices, or multidimensional arrays that have the same size, which is also the size of R. A scalar input for mu or sigma is expanded to a constant array with the same dimensions as the other input.
Martin Haugh, " Generating Random Variables and Stochastic Process", 2004 [1]
MathWorks [2]
Ozgur KARAYALCIN, "Generating Random numbers and Random variables", 2007 [3]