summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-11-23 10:22:13 (GMT)
committerGitHub <noreply@github.com>2019-11-23 10:22:13 (GMT)
commit041d8b48a2e59fa642b2c5124d78086baf74e339 (patch)
treef189a49ce0107fdeef9eeb75e9e6c63fb169e83c /Lib/random.py
parent84b1ff65609c5910b4f838adbe1ead83baae7dbf (diff)
downloadcpython-041d8b48a2e59fa642b2c5124d78086baf74e339.zip
cpython-041d8b48a2e59fa642b2c5124d78086baf74e339.tar.gz
cpython-041d8b48a2e59fa642b2c5124d78086baf74e339.tar.bz2
bpo-38881: choices() raises ValueError when all weights are zero (GH-17362)
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py4
1 files changed, 3 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py
index be4401c..e24737d 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -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)]