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@users.noreply.github.com>
diff --git a/Lib/random.py b/Lib/random.py
index 0bc2417..8e94064 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -361,7 +361,9 @@
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 -------------------