randn - Normally distributed pseudorandom numbers

Syntax

r = randn(n)
randn(m,n)
randn([m,n])
randn(m,n,p,...)
randn([m,n,p,...])
randn(size(A))
r = randn(..., 'double')
r = randn(..., 'single')

Description

r = randn(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard normal distribution. randn(m,n) or randn([m,n]) returns an m-by-n matrix. randn(m,n,p,...) or randn([m,n,p,...]) returns an m-by-n-by-p-by-... array. randn returns a scalar. randn(size(A)) returns an array the same size as A.

r = randn(..., 'double') or r = randn(..., 'single') returns an array of normal values of the specified class.

The sequence of numbers produced by randn is determined by the internal state of the uniform pseudorandom number generator that underlies rand, randi, and randn. randn uses one or more uniform values from that default stream to generate each normal value. Control the default stream using its properties and methods. See @RandStream for details about the default stream.

Resetting the default stream to the same fixed state allows computations to be repeated. Setting the stream to different states leads to unique computations, however, it does not improve any statistical properties. Since the random number generator is initialized to the same state every time MATLAB software starts up, rand, randn, and randi will generate the same sequence of numbers in each session until the state is changed.

Examples

Generate values from a normal distribution with mean 1 and standard deviation 2.

r = 1 + 2.*randn(100,1);

Generate values from a bivariate normal distribution with specified mean vector and covariance matrix.

mu = [1 2];
Sigma = [1 .5; .5 2]; R = chol(Sigma);
z = repmat(mu,100,1) + randn(100,2)*R;

Replace the default stream at MATLAB startup, using a stream whose seed is based on clock, so that randn will return different values in different MATLAB sessions. It is usually not desirable to do this more than once per MATLAB session.

RandStream.setDefaultStream
     (RandStream('mt19937ar','seed',sum(100*clock)));
randn(1,5)

Save the current state of the default stream, generate 5 values, restore the state, and repeat the sequence.

defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
z1 = randn(1,5)
defaultStream.State = savedState;
z2 = randn(1,5) % contains exactly the same values as z1

See Also

rand, randi, @RandStream, randn (RandStream), getDefaultStream (RandStream)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS