You are here : matlabSignal Processingrandn

randn() - Signal Processing

exampleX = randn returns a random scalar drawn
from the standard normal distribution.
exampleX = randn(n) returns
an n-by-n matrix of normally
distributed random numbers.
exampleX = randn(sz1,...,szN) returns
an sz1-by-...-by-szN array of
random numbers where sz1,...,szN indicate the size
of each dimension. For example, randn(3,4) returns
a 3-by-4 matrix.
exampleX = randn(sz) returns
an array of random numbers where size vector sz defines size(X).
For example, randn([3 4]) returns a 3-by-4 matrix.

example X = randn(___,typename) returns
an array of random numbers of data type typename.
The typename input can be either 'single' or 'double'.
You can use any of the input arguments in the previous syntaxes. 
exampleX = randn(___,'like',p) returns
an array of random numbers like p; that is, of
the same object type as p. You can specify either typename or 'like',
but not both.
The sequence of numbers produced by randn is
determined by the internal settings of the uniform pseudorandom number
generator that underlies rand, randi,
and randn. You can control that shared random number
generator using rng.Note:  


Use the rng function
instead of rand or randn with
the 'seed', 'state', or 'twister' inputs.
For more information, see Replace Discouraged
Syntaxes of rand and randn


Syntax

X = randn exampleX = randn(n) exampleX = randn(sz1,...,szN) exampleX = randn(sz) exampleX = randn(___,typename) exampleX = randn(___,'like',p) example


Example

Matrix of Random NumbersOpen This Example
Generate a 5-by-5 matrix of normally distributed random numbers.r = randn(5)

r =

    0.5377   -1.3077   -1.3499   -0.2050    0.6715
    1.8339   -0.4336    3.0349   -0.1241   -1.2075
   -2.2588    0.3426    0.7254    1.4897    0.7172
    0.8622    3.5784   -0.0631    1.4090    1.6302
    0.3188    2.7694    0.7147    1.4172    0.4889

Bivariate Normal Random NumbersOpen This Example
Generate values from a bivariate normal distribution with specified mean vector and covariance matrix.mu = [1 2];
sigma = [1 0.5; 0.5 2];
R = chol(sigma);
z = repmat(mu,10,1) + randn(10,2)*R

z =

    1.5377    0.4831
    2.8339    6.9318
   -1.2588    1.8302
    1.8622    2.3477
    1.3188    3.1049
   -0.3077    1.0750
    0.5664    1.6190
    1.3426    4.1420
    4.5784    5.6532
    3.7694    5.2595

Random Complex NumbersOpen This Example
Generate a single random complex number with normally distributed real and imaginary parts.a = randn + 1i*randn

a =

   0.5377 + 1.8339i

Reset Random Number GeneratorOpen This Example
Save the current state of the random number generator and create a 1-by-5 vector of random numbers.s = rng;
r = randn(1,5)

r =

    0.5377    1.8339   -2.2588    0.8622    0.3188

Restore the state of the random number generator to s, and then create a new 1-by-5 vector of random numbers. The values are the same as before.rng(s);
r1 = randn(1,5)

r1 =

    0.5377    1.8339   -2.2588    0.8622    0.3188

Always use the rng function (rather than the rand or randn functions) to specify the settings of the random number generator. For more information, see Replace Discouraged
Syntaxes of rand and randn.3-D Array of Random NumbersOpen This Example
Create a 3-by-2-by-3 array of random numbers.X = randn([3,2,3])

X(:,:,1) =

    0.5377    0.8622
    1.8339    0.3188
   -2.2588   -1.3077


X(:,:,2) =

   -0.4336    2.7694
    0.3426   -1.3499
    3.5784    3.0349


X(:,:,3) =

    0.7254   -0.2050
   -0.0631   -0.1241
    0.7147    1.4897

Specify Data Type of Random NumbersOpen This Example
Create a 1-by-4 vector of random numbers whose elements are single precision.r = randn(1,4,'single')

r =

    0.5377    1.8339   -2.2588    0.8622

class(r)

ans =

single

Clone Size from Existing ArrayOpen This Example
Create a matrix of normally distributed random numbers with the same size as an existing array.A = [3 2; -2 1];
sz = size(A);
X = randn(sz)

X =

    0.5377   -2.2588
    1.8339    0.8622

It is a common pattern to combine the previous two lines of code into a single line:X = randn(size(A));
Clone Size and Data Type from Existing ArrayOpen This Example
Create a 2-by-2 matrix of single precision random numbers.p = single([3 2; -2 1]);
Create an array of random numbers that is the same size and data type as p.X = randn(size(p),'like',p)

X =

    0.5377   -2.2588
    1.8339    0.8622

class(X)

ans =

single

Clone Distributed ArrayOpen This Example
If you have Parallel Computing Toolbox™, create a 1000-by-1000 distributed array of random numbers with underlying data type single. For the distributed data type, the 'like' syntax clones the underlying data type in addition to the primary data type.p = randn(1000,'single','distributed');
Starting parallel pool (parpool) using the 'local' profile ... connected to 12 workers.
Create an array of random numbers that is the same size, primary data type, and underlying data type as p.X = randn(size(p),'like',p);
class(X)

ans =

distributed

classUnderlying(X)

ans =

single

Related ExamplesCreate Arrays of Random NumbersRandom Numbers Within a Specific RangeRandom Numbers Within a SphereRandom Numbers from Normal Distribution with Specific Mean
and Variance


Output / Return Value


Limitations


Alternatives / See Also


Reference