MATLAB: How to obtain samples from a normal distribution (Gaussian) where the distribution has a chosen mean and variance

gaussgaussianMATLABmeannormalrandomvariance

The RANDN function only generates normal variables with a mean of zero and a variance of one. I would like to use it to generate normal variables with mean and variance equal to some values I choose.

Best Answer

You can transform the random numbers returned by RANDN, which are normally distributed with mean 0 and variance 1, to have whatever mean and variance you desire. If your desired mean is MU and your desired variance is VAR, the following code can be used to return a normally distributed random number with mean MU and variance VAR:
randn*sqrt(VAR)+MU
Alternately, if you have the Statistics Toolbox you can use the function NORMRND to generate this data. The Statistics Toolbox also contains functions for generating random numbers from other distributions, such as the Poisson, Binomial, or Exponential distributions. You can read more about the random number generation capabilities of the Statistics Toolbox here:
Related Question