在matlab中生成随机数偏向边界

时间:2015-10-05 07:00:05

标签: matlab probability

我想在matlab中生成有偏差的随机数。让我通过偏见的意思解释一下。 假设我有一个定义的上限和下限分别为30和10。 我想生成偏向边界的N个随机数,使得数字接近10和30(极值)的概率与位于中间的数字的概率相比更高。 我怎样才能做到这一点? 非常感谢任何帮助:)

2 个答案:

答案 0 :(得分:2)

% Upper bound
UB = 30
% Lower bound
LB = 0;
% Range
L = UB-LB;
% Std dev - you may want to relate it to L - maybe use sigma=sqrt(L)
sigma = L/6;
% Number of samples to generate
n = 1000000;
X = sigma*randn(1,n);
% Remove items that are above bounds - not sure if it's what you want.. if not comment the two following lines
X(X<-L) = [];
X(X>L) = [];

% Take values above zero for lower bounds, other values for upper bound
ii = X > 0;
X(ii) = LB + X(ii);
X(~ii) = UB + X(~ii);

% plot histogram
hist(X, 100);

enter image description here

我在这里使用了正态分布,但很明显你可以适应其他人......你也可以改变西格玛。

答案 1 :(得分:1)

要为任意分布生成随机数,您必须定义反转的cumulative distribution function。我们说你叫它myICDF。获得此功能后,您可以使用myICDF(rand(n,m))生成随机样本。