Setting min/max limits to the function random (not rand nor randn)?

I am using the function RANDOM to generate random numbers with a specific Kernel PDF that I created using FITDIST. Is there a way to control the min/max of the generated random numbers?
pd1 = fitdist((data(:,1)),'Kernel');
Y1 = random(pd1,100000,1);

 Accepted Answer

The easiest way to do this in your context is to restrict the actual PDF you're using to generate the random variable, rather than do a resampling of the random number. You can do this by calling the Support property:
pd = fitdist(x,'Kernel','Support',[100,250])
Where the bound of the support are in this example [100,250]. The random function will then not generate numbers outside this range.

3 Comments

Dear jgg,
Thank you for your time. I am not sure what is the "support" property I could not find anything straightforward on help. However, I have tried the line you wrote and I get the following error:
Error using prob.KernelDistribution (line 126) SUPPORT must be 'unbounded', 'positive', or a finite two-element sorted vector.
Error in prob.KernelDistribution.fit (line 78) pd = prob.KernelDistribution(varargin{:});
Error in fitdist>localfit (line 231) pd = feval(fitter,x,'cens',c,'freq',f,varargin{:});
Error in fitdist (line 178) pd = localfit(dist,fitter,x,cens,freq,args{:});
Error in IOARDANOFF_V2 (line 148) pd1 = fitdist((stat(:,1)),'Kernel','Support',[4,3])
Are you sure you executed this properly? You can read the documentation on the "support" property here. Scroll down and look for "support" under the name-value pair arguments section.
For instance, this code works as expected on my PC:
clear
load hospital
x = hospital.Weight;
pd = fitdist(x,'Kernel','Support',[100,250])
Y1 = random(pd,10000,1);
Ah, the issue with your code is that you should call it like:
pd1 = fitdist((stat(:,1)),'Kernel','Support',[3,4])
The support needs to be sorted: [lb,ub]

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 18 Jan 2016

Commented:

jgg
on 18 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!