From bb8d496cad96cf8391d78d38c50a37292ca74656 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 22 Nov 2019 20:04:28 -0800 Subject: [PATCH 1/3] bpo-38881: choices() raises ValueError when all weights are zero --- Lib/random.py | 4 +++- Lib/test/test_random.py | 5 +++++ .../next/Library/2019-11-22-20-03-46.bpo-38881.7HV1Q0.rst | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-11-22-20-03-46.bpo-38881.7HV1Q0.rst diff --git a/Lib/random.py b/Lib/random.py index be4401c554d7e5..e24737d4508a8f 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -413,8 +413,10 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1): 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)] diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index f59c5652b5ddaa..2c8c8e885452ad 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -241,6 +241,11 @@ def test_choices_subnormal(self): choices = self.gen.choices choices(population=[1, 2], weights=[1e-323, 1e-323], k=5000) + def test_choices_with_all_zero_weights(self): + # See issue #38881 + with self.assertRaises(ValueError): + self.gen.choices('AB', [0.0, 0.0]) + def test_gauss(self): # Ensure that the seed() method initializes all the hidden state. In # particular, through 2.2.1 it failed to reset a piece of state used diff --git a/Misc/NEWS.d/next/Library/2019-11-22-20-03-46.bpo-38881.7HV1Q0.rst b/Misc/NEWS.d/next/Library/2019-11-22-20-03-46.bpo-38881.7HV1Q0.rst new file mode 100644 index 00000000000000..9f4a27db4524f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-11-22-20-03-46.bpo-38881.7HV1Q0.rst @@ -0,0 +1 @@ +random.choices() now raises a ValueError when all the weights are zero. From 17c553de1ef36c9bcfb26197007c3e69707d0954 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 22 Nov 2019 20:10:54 -0800 Subject: [PATCH 2/3] Update docs --- Doc/library/random.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 1bd1856937be5b..9696e21b25197c 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -166,7 +166,8 @@ Functions for sequences The *weights* or *cum_weights* can use any numeric type that interoperates with the :class:`float` values returned by :func:`random` (that includes integers, floats, and fractions but excludes decimals). Weights are - assumed to be non-negative. + assumed to be non-negative. A :exc:`ValueError` is raised if the + total of all weights isn't positive. For a given seed, the :func:`choices` function with equal weighting typically produces a different sequence than repeated calls to @@ -177,6 +178,9 @@ Functions for sequences .. versionadded:: 3.6 + .. versionchanged:: 3.9 + Raises a :exc:`ValueError` if the total weights aren't more than zero. + .. function:: shuffle(x[, random]) From 9091dbae69c4b38b84fa746f90f93d1ee58673ba Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 23 Nov 2019 01:46:12 -0800 Subject: [PATCH 3/3] Focus on the case where all weights are zero. --- Doc/library/random.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 9696e21b25197c..933da3f8fcf650 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -165,9 +165,9 @@ Functions for sequences The *weights* or *cum_weights* can use any numeric type that interoperates with the :class:`float` values returned by :func:`random` (that includes - integers, floats, and fractions but excludes decimals). Weights are - assumed to be non-negative. A :exc:`ValueError` is raised if the - total of all weights isn't positive. + integers, floats, and fractions but excludes decimals). Behavior is + undefined if any weight is negative. A :exc:`ValueError` is raised if all + weights are zero. For a given seed, the :func:`choices` function with equal weighting typically produces a different sequence than repeated calls to @@ -179,7 +179,7 @@ Functions for sequences .. versionadded:: 3.6 .. versionchanged:: 3.9 - Raises a :exc:`ValueError` if the total weights aren't more than zero. + Raises a :exc:`ValueError` if all weights are zero. .. function:: shuffle(x[, random])