This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Serge Anuchin, mark.dickinson, pitrou, r.david.murray, rhettinger, serhiy.storchaka, skrah, steven.daprano, tim.peters, vstinner
Date 2015-07-11.22:52:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1436655156.31.0.101222333143.issue24567@psf.upfronthosting.co.za>
In-reply-to
Content
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)]
History
Date User Action Args
2015-07-11 22:52:36rhettingersetrecipients: + rhettinger, tim.peters, mark.dickinson, pitrou, vstinner, steven.daprano, r.david.murray, skrah, serhiy.storchaka, Serge Anuchin
2015-07-11 22:52:36rhettingersetmessageid: <1436655156.31.0.101222333143.issue24567@psf.upfronthosting.co.za>
2015-07-11 22:52:36rhettingerlinkissue24567 messages
2015-07-11 22:52:36rhettingercreate