[issue18844] allow weights in random.choice

Serhiy Storchaka report at bugs.python.org
Thu Sep 12 00:29:41 CEST 2013


Serhiy Storchaka added the comment:

The proposed patch add two methods to the Random class and two module level functions: weighted_choice() and weighted_choice_generator().

weighted_choice(data) accepts either mapping or sequence and returns a key or index x with probability which is proportional to data[x].

If you need several elements with same distribution, use weighted_choice_generator(data) which returns an iterator which produces random keys or indices of the data. It is more faster than calling weighted_choice(data) repeatedly and is more flexible than generating a list of random values at specified size (as in NumPy).

----------
Added file: http://bugs.python.org/file31732/weighted_choice_generator.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18844>
_______________________________________


More information about the Python-bugs-list mailing list