Last active
August 29, 2015 13:55
-
-
Save mdshw5/8774259 to your computer and use it in GitHub Desktop.
random.choice in Python 2.X vs 3.X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def choice(self, seq): | |
"""Choose a random element from a non-empty sequence.""" | |
return seq[int(self.random() * len(seq))] # raises IndexError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
return seq[i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment