Skip to content

Commit 041d8b4

Browse files
authored
bpo-38881: choices() raises ValueError when all weights are zero (GH-17362)
1 parent 84b1ff6 commit 041d8b4

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Doc/library/random.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ Functions for sequences
165165

166166
The *weights* or *cum_weights* can use any numeric type that interoperates
167167
with the :class:`float` values returned by :func:`random` (that includes
168-
integers, floats, and fractions but excludes decimals). Weights are
169-
assumed to be non-negative.
168+
integers, floats, and fractions but excludes decimals). Behavior is
169+
undefined if any weight is negative. A :exc:`ValueError` is raised if all
170+
weights are zero.
170171

171172
For a given seed, the :func:`choices` function with equal weighting
172173
typically produces a different sequence than repeated calls to
@@ -177,6 +178,9 @@ Functions for sequences
177178

178179
.. versionadded:: 3.6
179180

181+
.. versionchanged:: 3.9
182+
Raises a :exc:`ValueError` if all weights are zero.
183+
180184

181185
.. function:: shuffle(x[, random])
182186

Lib/random.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,10 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
413413
raise TypeError('Cannot specify both weights and cumulative weights')
414414
if len(cum_weights) != n:
415415
raise ValueError('The number of weights does not match the population')
416-
bisect = _bisect
417416
total = cum_weights[-1] + 0.0 # convert to float
417+
if total <= 0.0:
418+
raise ValueError('Total of weights must be greater than zero')
419+
bisect = _bisect
418420
hi = n - 1
419421
return [population[bisect(cum_weights, random() * total, 0, hi)]
420422
for i in _repeat(None, k)]

Lib/test/test_random.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ def test_choices_subnormal(self):
241241
choices = self.gen.choices
242242
choices(population=[1, 2], weights=[1e-323, 1e-323], k=5000)
243243

244+
def test_choices_with_all_zero_weights(self):
245+
# See issue #38881
246+
with self.assertRaises(ValueError):
247+
self.gen.choices('AB', [0.0, 0.0])
248+
244249
def test_gauss(self):
245250
# Ensure that the seed() method initializes all the hidden state. In
246251
# particular, through 2.2.1 it failed to reset a piece of state used
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
random.choices() now raises a ValueError when all the weights are zero.

0 commit comments

Comments
 (0)