Quantcast

Documentation Center

  • Trial Software
  • Product Updates

randn

Normally distributed pseudorandom numbers

Syntax

  • r = randn
  • r = randn(n)
  • r = randn(sz1,...,szN)
  • r = randn(sz)
  • r = randn(classname)
  • r = randn(n,classname)
  • r = randn(sz1,...,szN,classname)
  • r = randn(sz,classname)
  • r = randn('like',p)
  • r = randn(n,'like',p)
  • r = randn(sz1,...,szN,'like',p)
  • r = randn(sz,'like',p)

Description

r = randn returns a psuedorandom scalar drawn from the standard normal distribution.

r = randn(n) returns an n-by-n matrix containing pseudorandom normal values.

r = randn(sz1,...,szN) returns a sz1-by-...-by-szN array of pseudorandom normal values where sz1,...,szN indicates the size of each dimension. For example, randn(3,4) returns a 3-by-4 array of pseudorandom values.

r = randn(sz) returns an array of pseudorandom normal values where the size vector, sz, defines size(r). For example, randn([3,4]) returns a 3-by-4 array of pseudorandom values.

    Note   The size inputs sz1,...,szN, as well as the elements of the size vector sz, should be nonnegative integers. Negative integers are treated as 0.

r = randn(classname) returns a pseudorandom normal value where the string, classname, specifies the data type. classname can be either 'single' or 'double'.

r = randn(n,classname) returns an n-by-n array of pseudorandom normal values of data type classname.

r = randn(sz1,...,szN,classname) returns a sz1-by-...-by-szN array of pseudorandom normal values of data type classname.

r = randn(sz,classname) returns an array of pseudorandom normal values where the size vector, sz, defines size(r) and classname defines class(r).

r = randn('like',p) returns a pseudorandom normal value of the same data type as the numeric variable, p.

r = randn(n,'like',p) returns an n-by-n array of pseudorandom normal values like p.

r = randn(sz1,...,szN,'like',p) returns a sz1-by-...-by-szN array of pseudorandom normal values like p

r = randn(sz,'like',p) returns an array of pseudorandom normal values like p where the size vector, sz, defines size(r).

The sequence of numbers produced by randn is determined by the settings of the uniform random number generator that underlies rand, randn, and randi. randn uses one or more uniform random values to create each normal random value. You can control that shared random number generator using rng.

Examples

Example 1

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

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

Example 2

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;

Example 3

Reset the random number generator used by rand, randi, and randn to its default startup settings, so that randn produces the same random numbers as if you restarted MATLAB®:

rng('default');
randn(1,5)
ans =
    0.5377    1.8339   -2.2588    0.8622    0.3188

Example 4

Save the settings for the random number generator used by rand, randi, and randn, generate 5 values from randn, restore the settings, and repeat those values:

s = rng;
z1 = randn(1,5)
z1 =
   -1.3077   -0.4336    0.3426    3.5784    2.7694
rng(s);
z2 = randn(1,5)
z2 =
   -1.3077   -0.4336    0.3426    3.5784    2.7694

z2 contains exactly the same values as z1.

Example 5

Reinitialize the random number generator used by rand, randi, and randn with a seed based on the current time. randn returns different values each time you do this. Note that it is usually not necessary to do this more than once per MATLAB session:

rng('shuffle');
randn(1,5)

See Also

| | | |

Related Examples

Was this topic helpful?