mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
GH-100805: Support numpy.array() in random.choice(). (GH-100830)
This commit is contained in:
parent
87d3bd0e02
commit
9a68ff12c3
3 changed files with 21 additions and 1 deletions
|
@ -336,7 +336,10 @@ class Random(_random.Random):
|
|||
|
||||
def choice(self, seq):
|
||||
"""Choose a random element from a non-empty sequence."""
|
||||
if not seq:
|
||||
|
||||
# As an accommodation for NumPy, we don't use "if not seq"
|
||||
# because bool(numpy.array()) raises a ValueError.
|
||||
if not len(seq):
|
||||
raise IndexError('Cannot choose from an empty sequence')
|
||||
return seq[self._randbelow(len(seq))]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue