Random Numbers within specified range in specific ratio
Show older comments
Hi i want generate random numbers in this ratio
between
0-0.1 once........
0.1-0.2 twice........
0.2-0.3 thrice.......
0.3-0.4 4 times........
0.4-0.5 5 times........
0.5-0.6 6 times........
0.6-0.7 7 times........
0.7-0.8 8 times........
0.8-0.9 9 times........
0.9-1 10 times
out of total number of samples.... same ratio should be thr for any number of samples i change...
thank you
2 Comments
Walter Roberson
on 18 Dec 2012
To check: 0 to 0.1 should be generated in the ratio of 1/55, 0.1-0.2 should be generated in the ratio 2/55, up to 0.9-1 should be generated in the ratio 10/55 ? Since 1+2+3+4+5+6+7+8+9+10 = 55 ?
sp
on 18 Dec 2012
Answers (2)
Roger Stafford
on 18 Dec 2012
You haven't specified whether you want the density to be constant within each of these subintervals or be a continuous function throughout the entire interval from 0 to 1. If you want the latter, you can get n such random values with this:
r = (sqrt(440*rand(n,1)+1)-1)/20;
Note that the probability of being between 0 and .1 is 1/55, between .1 and .2 it is 2/55, between .2 and .3 it is 3/55, etc.
Roger Stafford
5 Comments
sp
on 18 Dec 2012
Image Analyst
on 18 Dec 2012
Roger shared this link with us some time ago: http://en.wikipedia.org/wiki/Inverse_transform_sampling
Walter Roberson
on 18 Dec 2012
Does that apply to each sub-interval of the output? So that if location #20 is the one that has the 0-0.1, then location #75 exactly would also have to be 0-0.1 because otherwise the subinterval 20:74 would have too many 0-0.1 (if the second 0-0.1 was before #75) or the subinterval 21:75 would have too few 0.01 (if the second 0-0.1 was after #75) ?
Roger Stafford
on 18 Dec 2012
Edited: Walter Roberson
on 18 Dec 2012
If I correctly interpret what you are saying, Praveen, then you are mistaken. The probability that an element of rand(n,1) will fall between 1/55 and 3/55 is their difference, 3/55-1/55=2/55. The corresponding values of r at these two limits are
r1 = (sqrt(440*1/55+1)-1)/20 = .1
r2 = (sqrt(440*3/55+1)-1)/20 = .2
Thus, the probability that r will fall between .1 and .2 is 2/55, which is just what you are asking for. The same applies to all other nine intervals.
Roger Stafford
Walter Roberson
on 18 Dec 2012
I think Praveen is looking for exactly 2, exactly 6, and so on, in an interval of 55, rather than "2 as a statistical average", "6 as a statistical average", and so on.
Image Analyst
on 18 Dec 2012
See if this is what you want:
r = rand(100000, 1);
binEdges = [0 1, 3, 6, 10, 15, 21, 28, 36, 45, 55] / 55
counts = histc(r, binEdges);
bar(counts);
grid on;
Categories
Find more on Creating and Concatenating Matrices 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!