choosing random numbers with weights/probability?

Al Christians achrist at easystreet.com
Mon Jun 21 20:08:48 EDT 1999


The standard way to do this (with non-integer weights) is to have
a weight associated with each choice, normalized (scaled) so that the
maximum weight is equal to 1.0.  Then, after randomly selecting an item
from the collection, generate a uniform random number  between 0.0 and
1.0 and accept the choice only if the second random number is less 
than the weight of the randomly selected item.  If it is not, the item
is not used and you have to repeat the entire process with new
random numbers as many times as needed to get an item for which the
weight exeeds the second random number.

Al
 

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..
> 
> thanks,
> 
> Kevin
> 
> **** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ****




More information about the Python-list mailing list