How to generate random numbers from a normal distribution with specific mean and variance?

时间秒杀一切 提交于 2019-11-29 22:56:45

问题


I need to generate a Gaussian random sample of n numbers, with mean 0 and variance 1, using the randn function.

In general, how would I generate a Gaussian random sample X of n numbers, with mean mu and variance v, using the randn function?


回答1:


A standard normal distribution already has mean 0 and variance 1.

If you want to change the mean, just "translate" the distribution, i.e., add your mean value to each generated number. Similarly, if you want to change the variance, just "scale" the distribution, i.e., multiply all your numbers by sqrt(v). For example,

v = 1.5; % variance
sigma = sqrt(v); % standard deviation
mu = 2; % mean
n = 1000
X = sigma .* randn(n, 1) + mu;
stats = [mean(X) std(X) var(X)]

See the following article:

https://ch.mathworks.com/help/matlab/math/random-numbers-with-specific-mean-and-variance.html

for more info.




回答2:


You could also call

normrnd(0,1,[M,N])

or

random('Normal',0,1,[M,N])


来源:https://stackoverflow.com/questions/15884945/how-to-generate-random-numbers-from-a-normal-distribution-with-specific-mean-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!