From: Raymond Hettinger Date: Fri, 1 May 2020 17:34:19 +0000 (-0700) Subject: Simplify choice()'s interaction with the private _randbelow() method (GH-19831) X-Git-Tag: v3.9.0b1~216 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4168f1e46041645cf54bd053981270d8c4c1313b;p=thirdparty%2FPython%2Fcpython.git Simplify choice()'s interaction with the private _randbelow() method (GH-19831) --- diff --git a/Lib/random.py b/Lib/random.py index 80fe447db6c8..8f840e1abb90 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -265,10 +265,10 @@ class Random(_random.Random): return self.randrange(a, b+1) def _randbelow_with_getrandbits(self, n): - "Return a random int in the range [0,n). Raises ValueError if n==0." + "Return a random int in the range [0,n). Returns 0 if n==0." if not n: - raise ValueError("Boundary cannot be zero") + return 0 getrandbits = self.getrandbits k = n.bit_length() # don't use (n-1) here because n can be 1 r = getrandbits(k) # 0 <= r < 2**k @@ -277,7 +277,7 @@ class Random(_random.Random): return r def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<