> On Thu, Dec 2, 2010 at 12:41 PM, raymond.hettinger
> <python-check...@python.org> wrote:
>> +A more general approach is to arrange the weights in a cumulative 
>> probability
>> +distribution with :func:`itertools.accumulate`, and then locate the random 
>> value
>> +with :func:`bisect.bisect`::
>> +
>> +    >>> choices, weights = zip(*weighted_choices)
>> +    >>> cumdist = list(itertools.accumulate(weights))
>> +    >>> x = random.random() * cumdist[-1]
>> +    >>> choices[bisect.bisect(cumdist, x)]
>> +    'Blue'

“pydoc bisect.bisect” is empty (“Alias for bisect_right()”); in the
code, bisect.bisect is noted as compatibility alias.  Wouldn’t it be
more helpful to use the newer name?

Regards

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to