Biased random?

Jeffrey Barish jeff_barish at
Tue Aug 28 22:23:41 CEST 2007

Ivan Voras wrote:

> Hi,
> I have a list of items, and need to choose several elements from it,
> "almost random". The catch is that the elements from the beginning
> should have more chance of being selected than those at the end (how
> much more? I don't care how the "envelope" of probability looks like at
> this point - can be linear). I see that there are several functions in
> Python standard libraries for various distribution, but is there an easy
> pythonic way to make them do what I need?

If you take the difference between two uniformly distributed random
variables, the probability density function forms an isosceles triangle
centered at 0.  Take the absolute value of that variable and the pdf is a
straight line with maximum value at 0 tapering to 0 at max.  Thus,

z = abs(randint(0, max) - randint(0, max))

ought to do the trick.
Jeffrey Barish

More information about the Python-list mailing list