I want to generate biased random numbers in matlab. Let me explain a bit more, by what I mean by biased. Lets say I have a defined upper bound and lower bound of 30 and 10 respectively. I want to generate N random numbers biased towards the bounds, such that the probability of the numbers lying close to 10 and 30 (the extremes) is more as compared to them lying some where in the middle. How can I do this? Any help is much appreciated :)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
% 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);
I used a normal distribution here but obviously you can adapt to use others.. you can change the sigma also.
回答2:
To generate random numbers for an arbitrary distribution, you have to define the inverted cumulative distribution function. Let's say you called it myICDF
. Once you got this function, you can generate random samples using myICDF(rand(n,m))
.