mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
bpo-38881: choices() raises ValueError when all weights are zero (GH-17362)
This commit is contained in:
parent
84b1ff6560
commit
041d8b48a2
4 changed files with 15 additions and 3 deletions
|
@ -413,8 +413,10 @@ class Random(_random.Random):
|
|||
raise TypeError('Cannot specify both weights and cumulative weights')
|
||||
if len(cum_weights) != n:
|
||||
raise ValueError('The number of weights does not match the population')
|
||||
bisect = _bisect
|
||||
total = cum_weights[-1] + 0.0 # convert to float
|
||||
if total <= 0.0:
|
||||
raise ValueError('Total of weights must be greater than zero')
|
||||
bisect = _bisect
|
||||
hi = n - 1
|
||||
return [population[bisect(cum_weights, random() * total, 0, hi)]
|
||||
for i in _repeat(None, k)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue