This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients Serge Anuchin, mark.dickinson, pitrou, r.david.murray, rhettinger, serhiy.storchaka, skrah, steven.daprano, tim.peters, vstinner
Date 2016-09-10.07:54:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473494045.22.0.72285980455.issue24567@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2016-09-10 07:54:05mark.dickinsonsetrecipients: + mark.dickinson, tim.peters, rhettinger, pitrou, vstinner, steven.daprano, r.david.murray, skrah, serhiy.storchaka, Serge Anuchin
2016-09-10 07:54:05mark.dickinsonsetmessageid: <1473494045.22.0.72285980455.issue24567@psf.upfronthosting.co.za>
2016-09-10 07:54:05mark.dickinsonlinkissue24567 messages
2016-09-10 07:54:05mark.dickinsoncreate