[issue1551113] random.choice(setinstance) fails

Rob Renaud report at bugs.python.org
Fri Mar 27 06:05:11 CET 2009


Rob Renaud <rrenaud at google.com> added the comment:

I found this via google search when disappointed that random.choice
raised an exception rather than returned a random item in the set.

It's quite easy to implement random.choice for sets/dicts in O(1)
expected time from the C implementation as long as the set/dict
implementation guarantees minimal constant density.  Simply generate
random indices in the set object until one with an object is found . 
This has will work in expected O(1/density) probes.

I suppose making random.choice work for sets/dicts isn't worth a C
implementation (as happy as it would have made me a few hours ago...)?

----------
nosy: +rrenaud

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


More information about the Python-bugs-list mailing list