difference between random module in python 2.6 and 3.2?

Matej Cepl mcepl at redhat.com
Mon Feb 6 23:06:04 CET 2012


On 6.2.2012 20:26, Tim Chase wrote:
> In an ideal world, the code wouldn't have broken backwards compat.
> However, given the conditions, if Matej is willing to forgo bug-fixes,
> it's a reasonable solution. The alternate might be to try moving the
> recent/fixed version into the old project and updating tests/data to
> work with it. I have some 2.4 production code in which I've pulled 2.6's
> zipfile module in to give access to the iterator access (rather than the
> .read() method which tries to read in umpteen gigs of data that I just
> want to spool out to a file on disk).

Given I really don't care that much about distribution of my choice, 
this function

     def _choice(self, seq):
         """Choose a random element from a non-empty sequence.

         Embedding random.choice from 2.6 in order to get an uniform
         results between 2.6 and 3.2, where random module has been
         changed because of http://bugs.python.org/issue9025.
         See also http://groups.google.com/group/comp.lang.python\
         /browse_thread/thread/2b000b8ca8c5e98e

         Raises IndexError if seq is empty
         """
         return seq[int(random.random() * len(seq))]

doesn't seem like something so terrible (and maintenance intense). :)

Thanks for the help though

Matěj



More information about the Python-list mailing list