Mark Dickinson added the comment:

A similar bug affects the new `choices` method: in the `choices` source, if 
`random() * total` happens to round up to `total`, the bisect call returns an 
out-of-range index.

There are two ways that that could happen: (1) double rounding, as in this 
issue (which will occur very rarely and is hard to reproduce), and (2) `total` 
being subnormal (easy to reproduce, but unlikely to occur in practice).

>>> from random import choices
>>> choices(500, population=[1, 2], weights=[1e-323, 1e-323])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Python/cpython/Lib/random.py", line 360, in choices
    return [population[bisect(cum_weights, random() * total)] for i in range(k)]
  File "/Users/mdickinson/Python/cpython/Lib/random.py", line 360, in <listcomp>
    return [population[bisect(cum_weights, random() * total)] for i in range(k)]
IndexError: list index out of range

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24567>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to