Probability selection algorithm
Paul Rubin
phr-n2003b at NOSPAMnightsong.com
Sun Feb 2 18:08:36 EST 2003
Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:
> I must be confused then -- the original post essentially
> wanted a way to generate a random index from a list of unequal-sized
> bins (very loosely paraphrased). But if you were to call the random
> function on each loop pass, doesn't that bias the results toward the
> first bins?
No, you weight the probabilities to make it come out right.
> Say the first random came out to be 0.999999..., which should select
> the last bin (regardless of how many bins there are, if the range is
> 0.0 <= random < 1.0). Your loop would fail on the first test (random
> < bin1), then you generate a new random number (say 0.1) and test
> that against the sum of bin1 and bin2, and now it passes, so you
> return bin2.
I don't really understand what you're getting at here. The (2nd)
example I posted gives correct results, I think. But it uses more
random numbers than it needs to.
More information about the Python-list
mailing list