Skip to content

Simplify choice()'s interaction with the private _randbelow() method #19831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ def randint(self, a, b):
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
Expand All @@ -277,7 +277,7 @@ def _randbelow_with_getrandbits(self, n):
return r

def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<<BPF):
"""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.

The implementation does not use getrandbits, but only random.
"""
Expand All @@ -289,7 +289,7 @@ def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<<BPF):
"To remove the range limitation, add a getrandbits() method.")
return int(random() * n)
if n == 0:
raise ValueError("Boundary cannot be zero")
return 0
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
r = random()
Expand All @@ -303,11 +303,7 @@ def _randbelow_without_getrandbits(self, n, int=int, maxsize=1<<BPF):

def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
try:
i = self._randbelow(len(seq))
except ValueError:
raise IndexError('Cannot choose from an empty sequence') from None
return seq[i]
return seq[self._randbelow(len(seq))] # raises IndexError if seq is empty

def shuffle(self, x, random=None):
"""Shuffle list x in place, and return None.
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,10 @@ def test_randbelow_without_getrandbits(self):
maxsize+1, maxsize=maxsize
)
self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize)
# issue 33203: test that _randbelow raises ValueError on
# issue 33203: test that _randbelow returns zero on
# n == 0 also in its getrandbits-independent branch.
with self.assertRaises(ValueError):
self.gen._randbelow_without_getrandbits(0, maxsize=maxsize)
x = self.gen._randbelow_without_getrandbits(0, maxsize=maxsize)
self.assertEqual(x, 0)

# This might be going too far to test a single line, but because of our
# noble aim of achieving 100% test coverage we need to write a case in
Expand Down