http://www.mathworks.com/matlabcentral/newsreader/view_thread/320309
MATLAB Central Newsreader  Random Integers with unequal chance
Feed for thread: Random Integers with unequal chance
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 22 May 2012 16:20:07 +0000
Random Integers with unequal chance
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320309#877597
Nick
Hi. I am trying to create random integer generator that gives a better chance of generating a lower number.<br>
<br>
The chance I am using are based on (0.5)^x<br>
<br>
So I would like the generator to have a 50% chance of giving a 1, 25% chance of giving a 2, 12.5% chance of giving a 3 and so on.<br>
<br>
<br>
I am cutting the generator off at 20.<br>
So the function<br>
randi ([1,20],[1,1]);<br>
is what I am starting from. Someone suggested giving each range their own chance of occuring. So I guess I would use an 'If" statement involving randi (1)>=0.5, randi(2)>=0.25....etc.<br>
<br>
But I am a bit lost and drabbled on how to go about starting this script. You don't have to make the whole thing I just need help getting started or a direction to go in. <br>
Thanks!

Tue, 22 May 2012 19:56:07 +0000
Re: Random Integers with unequal chance
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320309#877624
Roger Stafford
"Nick" wrote in message <jpgebn$gqe$1@newscl01ah.mathworks.com>...<br>
> So I would like the generator to have a 50% chance of giving a 1, 25% chance of giving a 2, 12.5% chance of giving a 3 and so on.<br>
           <br>
x = floor(log(rand(n,1)/2)/log(2));<br>
<br>
Roger Stafford

Tue, 22 May 2012 20:26:43 +0000
Re: Random Integers with unequal chance
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320309#877630
ImageAnalyst
In general, for some arbitrary function, follow these directions:<br>
<br>
<a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling">http://en.wikipedia.org/wiki/Inverse_transform_sampling</a><br>
<br>
(It might have been Roger that first told me about that link long ago.)