choosing random numbers with weights/probability?

Nathan Clegg nathan at islanddata.com
Mon Jun 21 14:06:48 EDT 1999


list = [('one', 25), ('two', 25), ('three', 50)]
l = []
for x, y in list:
        l.extend([x] * y)
item=whrandom.choice(list)

Too bulky?


On 21-Jun-99 kevinsl wrote:
> I've been using the whrandom.choice routine and it's very
> useful. But is there any way to add weights or
> probabilities of being chosen to the items in the list?
> 
> example:
> 
> list=['one','two','three']
> item=whrandom.choice(list)
> 
> Is there any way to say that 'one' and 'two' have a 25%
> chance of being chosen, and 'three' has a 50% chance?
> 
> I'm hoping there's already a module to do this... or else
> I'll be writing my own..


----------------------------------
Nathan Clegg
 nathan at islanddata.com






More information about the Python-list mailing list