如何使用randn来生成从0到10的随机数

时间:2011-12-04 19:24:20

标签: matlab

我正在尝试使用randn

从0到10生成10个随机数

我知道你可以使用rand

来做到这一点
 fix(rand(1,10) .* (ones(1,10)*11)

但我怎么能用randn做到这一点?

提前致谢

2 个答案:

答案 0 :(得分:0)

没有任何明智的方式。

一种可能性是:

mod(fix(randn(1,10)), 11);

但这不会给你一个统一的分布。

您还可以使用probability integral transform将一个分发映射到另一个分发。

答案 1 :(得分:0)

您可以使用Normal分布的累积和反向累积分布函数来生成给定范围内的随机数。

如果您有统计工具箱,则可以使用:

% Generate 10 values from uniform distribution on [0 1]
Y = rand(10,1);

% Find cumulative probabilities of 0 and 10
L = cdf('Normal', [0 10], mu, sigma)

% Generate uniformly distributed values on [L(1), L(2)].
R = L(1) + (L(2)-L(1))*Y;

% Random numbers within the range [0 10] from normal distribution
X = icdf('Normal', R, mu, sigma)
Xint = round(X)

如果您没有工具箱,则可以使用erfcinverfc代替icdfcdf进行缩放。有关详细信息,请参阅erfcinv文档。

相关问题