Biased random?

Dan Sommers me at privacy.net
Tue Aug 28 06:13:49 EDT 2007


On Mon, 27 Aug 2007 22:42:45 +0200, Ivan Voras wrote:

> 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 really just want to tend towards the beginning of the list, and
don't care what the envelope looks like, how about this:

    def biasedselection(thelist):
        index = random.random() * random.random() * len(thelist)
        # or index random.random() ** 2 * len(thelist)
        return thelist[index]

Dan

-- 
Dan Sommers                                   A death spiral goes clock-
<http://www.tombstonezero.net/dan/>           wise north of the equator.
Atoms are not things. -- Werner Heisenberg              -- Dilbert's PHB



More information about the Python-list mailing list