[Python-checkins] bpo-24567: Random subnormal.diff (GH-7954) (GH-7955)

Raymond Hettinger webhook-mailer at python.org
Wed Jun 27 04:53:14 EDT 2018


https://github.com/python/cpython/commit/0eaf7b975bd61169a8d78945d2d12f23299f61a8
commit: 0eaf7b975bd61169a8d78945d2d12f23299f61a8
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2018-06-27T01:53:04-07:00
summary:

bpo-24567: Random subnormal.diff (GH-7954) (GH-7955)

Handle subnormal weights for choices()
(cherry picked from commit ddf7171911e117aa7ad4b0f9ded4f0c3a4ca0fec)

Co-authored-by: Raymond Hettinger <rhettinger at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2018-06-27-00-31-30.bpo-24567.FuePyY.rst
M Lib/random.py
M Lib/test/test_random.py

diff --git a/Lib/random.py b/Lib/random.py
index 0bc24174e13f..8e94064c9c61 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -361,7 +361,9 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
             raise ValueError('The number of weights does not match the population')
         bisect = _bisect.bisect
         total = cum_weights[-1]
-        return [population[bisect(cum_weights, random() * total)] for i in range(k)]
+        hi = len(cum_weights) - 1
+        return [population[bisect(cum_weights, random() * total, 0, hi)]
+                for i in range(k)]
 
 ## -------------------- real-valued distributions  -------------------
 
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index eee245df48a1..cbf3e41b94a2 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -227,6 +227,14 @@ def test_choices(self):
         with self.assertRaises(IndexError):
             choices([], cum_weights=[], k=5)
 
+    def test_choices_subnormal(self):
+        # Subnormal weights would occassionally trigger an IndexError
+        # in choices() when the value returned by random() was large
+        # enough to make `random() * total` round up to the total.
+        # See https://bugs.python.org/msg275594 for more detail.
+        choices = self.gen.choices
+        choices(population=[1, 2], weights=[1e-323, 1e-323], k=5000)
+
     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/2018-06-27-00-31-30.bpo-24567.FuePyY.rst b/Misc/NEWS.d/next/Library/2018-06-27-00-31-30.bpo-24567.FuePyY.rst
new file mode 100644
index 000000000000..d496f2bc411c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-06-27-00-31-30.bpo-24567.FuePyY.rst
@@ -0,0 +1,2 @@
+Improve random.choices() to handle subnormal input weights that could
+occasionally trigger an IndexError.



More information about the Python-checkins mailing list