Raymond Hettinger added the comment:

FWIW, here are some variants (with differing degrees of brevity, clarity, and 
performance):

   def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        n = len(seq)
        i = int(self.random() * n)
        if i == n:
            i = n - 1
        return seq[i]

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        try:
            return seq[int(self.random() * len(seq))]                           
                                                                              
        except IndexError:
            return seq[-1]

    def choice(self, seq):
        """Choose a random element from a non-empty sequence."""
        n = len(seq)
        return seq[min(int(self.random() * n), n-1)]

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24567>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to