Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19887
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: difference between random module in python 2.6 and 3.2? |
| Date | 2012-02-06 00:07 -0500 |
| References | <jgna4q$1t08$1@ns.felk.cvut.cz> <4f2f5081$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5464.1328504850.27778.python-list@python.org> (permalink) |
On 2/5/2012 11:01 PM, Steven D'Aprano wrote:
> Reading the docs, I would expect that when using an int as seed, you
> should get identical results.
That is similar to expecting hash to be consistent from version to version.
> There is no mention that the PRNG has changed between 2.6 and 3.2;
There is at best an informal policy. This was discussed in
http://bugs.python.org/issue9025
Antoine argued that if there were a written policy, it should be limited
to bug-fix releases within a version. I agree.
> It appears to be a bug in 3.2, because 3.1 gives the same results as 2.6:
This change is a side effect of fixing the bug of non-uniformity
discussed in that issue. In any case, in 2.7 and probably 3.1:
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
return seq[int(self.random() * len(seq))] # raises IndexError
whereas in 3.2:
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]
The change was announced in What's New in 3.2
random
The integer methods in the random module now do a better job of
producing uniform distributions. Previously, they computed selections
with int(n*random()) which had a slight bias whenever n was not a power
of two. Now, multiple selections are made from a range up to the next
power of two and a selection is kept only when it falls within the range
0 <= x < n. The functions and methods affected are randrange(),
randint(), choice(), shuffle() and sample().
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
difference between random module in python 2.6 and 3.2? Matej Cepl <mcepl@redhat.com> - 2012-02-06 02:27 +0100
Re: difference between random module in python 2.6 and 3.2? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-06 04:01 +0000
Re: difference between random module in python 2.6 and 3.2? Terry Reedy <tjreedy@udel.edu> - 2012-02-06 00:07 -0500
Re: difference between random module in python 2.6 and 3.2? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-06 05:56 +0000
Re: difference between random module in python 2.6 and 3.2? Terry Reedy <tjreedy@udel.edu> - 2012-02-06 02:27 -0500
Re: difference between random module in python 2.6 and 3.2? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-06 08:05 +0000
Re: difference between random module in python 2.6 and 3.2? Matej Cepl <mcepl@redhat.com> - 2012-02-06 09:45 +0100
Re: difference between random module in python 2.6 and 3.2? Matej Cepl <mcepl@redhat.com> - 2012-02-06 09:57 +0100
Re: difference between random module in python 2.6 and 3.2? Tim Chase <python.list@tim.thechases.com> - 2012-02-06 13:26 -0600
Re: difference between random module in python 2.6 and 3.2? Matej Cepl <mcepl@redhat.com> - 2012-02-06 23:06 +0100
Re: difference between random module in python 2.6 and 3.2? Serhiy Storchaka <storchaka@gmail.com> - 2012-02-07 12:05 +0200
Re: difference between random module in python 2.6 and 3.2? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-02-07 16:11 +0100
Re: difference between random module in python 2.6 and 3.2? Mel Wilson <mwilson@the-wire.com> - 2012-02-06 10:20 -0500
csiph-web