Message275594
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 |
|
Date |
User |
Action |
Args |
2016-09-10 07:54:05 | mark.dickinson | set | recipients:
+ mark.dickinson, tim.peters, rhettinger, pitrou, vstinner, steven.daprano, r.david.murray, skrah, serhiy.storchaka, Serge Anuchin |
2016-09-10 07:54:05 | mark.dickinson | set | messageid: <1473494045.22.0.72285980455.issue24567@psf.upfronthosting.co.za> |
2016-09-10 07:54:05 | mark.dickinson | link | issue24567 messages |
2016-09-10 07:54:05 | mark.dickinson | create | |
|