diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-04-05 16:02:12 (GMT) |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2018-04-05 16:02:12 (GMT) |
commit | baf304e82e1b54dbeee6b78ddf168e33ed8d557a (patch) | |
tree | 502f940b1263b11be58f59249c7f6d3ac1b7072e /Lib/random.py | |
parent | 83f564fd242c1af9dbe6d4273ad50586489000e3 (diff) | |
download | cpython-baf304e82e1b54dbeee6b78ddf168e33ed8d557a.zip cpython-baf304e82e1b54dbeee6b78ddf168e33ed8d557a.tar.gz cpython-baf304e82e1b54dbeee6b78ddf168e33ed8d557a.tar.bz2 |
bpo-33203: Ensure random.choice always raises IndexError on empty sequence (GH-6338) (GH-6387)
(cherry picked from commit 091e95e9004b794280ab35becec2c3e30dd5e96e)
Co-authored-by: Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/Lib/random.py b/Lib/random.py index 91065b7..0bc2417 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -242,6 +242,8 @@ class Random(_random.Random): "enough bits to choose from a population range this large.\n" "To remove the range limitation, add a getrandbits() method.") return int(random() * n) + if n == 0: + raise ValueError("Boundary cannot be zero") rem = maxsize % n limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0 r = random() |