如何从具有特定均值和方差的正态分布中生成随机数? [英] How to generate random numbers from a normal distribution with specific mean and variance?

查看:1042
本文介绍了如何从具有特定均值和方差的正态分布中生成随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用randn函数生成均值为0且方差为1的n个数字的高斯随机样本.

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

通常,我如何使用randn函数生成具有n个数字,均值mu和方差v的高斯随机样本X?

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

推荐答案

A standard正态分布 已经具有均值0和方差1.

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

如果您要更改均值,只需转换"分布即可,即,将平均值添加到每个生成的数字中.同样,如果您要更改方差,只需缩放"分布,即将所有数字乘以sqrt(v).例如,

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)]

请参阅以下文章:

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

了解更多信息.

这篇关于如何从具有特定均值和方差的正态分布中生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆