Random Number Generator, lower numbers have higher chance
15 views (last 30 days)
Show older comments
Hi. I am trying to create a script for generating a random number, with lower numbers having a higher chance of being picked.
This will be in the range of 1 to 20
The percent chance is going to be based on (0.5)^n
So the chance of getting a 1 is 50%, the chance of getting a 2 is 25%, 3 is 12.5% and so on....
I am having trouble getting started with this. I'm not even sure if there is a way. You don't have to write this out, I would just like someone to help me get started or send me in the right direction.
0 Comments
Accepted Answer
More Answers (2)
owr
on 22 May 2012
If you have a recent version of MATLAB (2012A) and the Statistics Toolbox check out the function "datasample" with the optional parameter 'Weights'.
If not, try using "rand" which generates random numbers uniformly on interval [0,1]. If number is between 0 and 0.5, call it a "1", between 0.5 and 0.75, call it a "2", etc. "histc" should help with this by setting your "edges" to the cumulative sum of your probabilities, ie, 0.5, 0.75, 0.875,...
Thats hwo I would proceed. There might be something pre-built on the file Exchange that does this, or a MATLAB function Im missing.
Good luck.
0 Comments
Stephen
on 22 May 2012
just create the distribution
n=1:20;
dist = [];
for t=1:20
dist = [dist, n(t) * ones(1, round( 20/n(t) ))];
end
then dist(randi(numel(dist))) would give you your weighted answer
you can check hist(dist(randi(numel(dist,1e5,1))),100) to see that the probability is close enough
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!