Generating random numbers in matlab biased towards the boundaries

南笙酒味 提交于 2019-12-25 03:02:57

问题


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



来源:https://stackoverflow.com/questions/32942905/generating-random-numbers-in-matlab-biased-towards-the-boundaries

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