Creates an array of pseudo-random numbers of the specified size.
The numbers are normally distributed with zero mean and a unit
standard deviation (i.e., mu = 0, sigma = 1
).
Two seperate syntaxes are possible. The first syntax specifies the array
dimensions as a sequence of scalar dimensions:
y = randn(d1,d2,...,dn).
The resulting array has the given dimensions, and is filled with
random numbers. The type of y
is double
, a 64-bit floating
point array. To get arrays of other types, use the typecast
functions.
The second syntax specifies the array dimensions as a vector, where each element in the vector specifies a dimension length:
y = randn([d1,d2,...,dn]).
This syntax is more convenient for calling randn
using a
variable for the argument.
Recall that the probability density function (PDF) of a normal random variable is
The Gaussian random numbers are generated from pairs of uniform random numbers using a transformation technique.
The following example demonstrates an example of using the first form of the randn
function.
--> randn(2,2,2) ans = <double> - size: [2 2 2] (:,:,1) = Columns 1 to 2 -0.0361639933961680 0.693389551907565 -0.140415140955028 -0.238187257168569 (:,:,2) = Columns 1 to 2 0.599755385896831 -0.939406097470966 0.708649351074680 -0.00648807006806828
The second example demonstrates the second form of the randn
function.
--> randn([2,2,2]) ans = <double> - size: [2 2 2] (:,:,1) = Columns 1 to 2 -0.0361639933961680 0.693389551907565 -0.140415140955028 -0.238187257168569 (:,:,2) = Columns 1 to 2 0.599755385896831 -0.939406097470966 0.708649351074680 -0.00648807006806828
In the next example, we create a large array of 10000 normally distributed pseudo-random numbers. We then shift the mean to 10, and the variance to 5. We then numerically calculate the mean and variance using mean
and var
, respectively.
--> x = 10+sqrt(5)*randn(1,10000); --> mean(x) ans = <double> - size: [1 1] 10.0433689745839 --> var(x) ans = <double> - size: [1 1] 4.925273668042298