[issue24567] random.choice IndexError due to double-rounding

Serhiy Storchaka report at bugs.python.org
Sun Jul 12 08:57:23 CEST 2015


Serhiy Storchaka added the comment:

Unfortunately a link to Rietveld is not available for review and inlined comments.

In sample() the selected set can be initialized to {n} to avoid additional checks in the loop for large population. In the branch for small population, non-empty pool can be extended ("pool.append(pool[-1])") to avoid additional check in the loop.

In choice() I would write the condition as "i == n > 0" to avoid indexing with negative index. Custom collection can has non-standard behavior with negative indices. This doesn't add additional cost in normal case.

----------

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


More information about the Python-bugs-list mailing list