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 <rep...@bugs.python.org>
<http://bugs.python.org/issue18844>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to