Normally distributed pseudorandom numbers
r = randn(n)
r = randn(m,n)
r = randn([m,n])
r = randn(m,n,p,...)
r = randn([m,n,p,...])
r = randn
r = randn(size(A))
r = randn(...,'double')
r = randn(...,'single')
r = randn(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard normal distribution.
r = randn(m,n) or r = randn([m,n]) returns an m-by-n matrix.
r = randn(m,n,p,...) or r = randn([m,n,p,...]) returns an m-by-n-by-p-by-... array.
r = randn returns a scalar.
r = 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.
Note: The size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0.
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.
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;
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
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.
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: