How to generate random numbers which follow an arbitrary frequency distribution?
9 views (last 30 days)
Show older comments
I need to generate a vector of random numbers (say 100 numbers) between 0 and 1 which follow a particular trend.Suppose I have a histogram (not data, just the picture) more or less like the one below. Now, the random numbers that I need must have a frequency distribution similar to that shown by this histogram... That is I need more of the numbers to fall between .9 and 1. And I need this to work for any given frequency distribution.. Is it possible? I am very, very new to MATLAB.
0 Comments
Accepted Answer
ES
on 8 Jan 2014
Edited: ES
on 8 Jan 2014
segments=[0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]; %Required segments from your hist image above
counts=[0,2,4,6,7,10,14,0,1,0]; %Count of required elements in each segment NOTE: length(counts)=length(segments)-1
r=[];%Random Matrix output
for l1=1:length(segments)-1
a=segments(l1);%start Segment Value
b=segments(l1+1); %end segment Value
r = [r (a + (b-a).*rand(counts(l1),1))']; %Generate random nos within the range
and append to output matrix
end
r=r(randperm(sum(counts)))%Randomize the output matrix!
hist(r);%optional
0 Comments
More Answers (0)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!