Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19887

Re: difference between random module in python 2.6 and 3.2?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'bug': 0.02; 'subject:module': 0.04; 'computed': 0.07; 'raises': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'valueerror:': 0.09; 'subject:python': 0.10; 'def': 0.13; '2.7': 0.13; '2.6:': 0.16; '3.2,': 0.16; 'agree.': 0.16; 'antoine': 0.16; 'docs,': 0.16; 'non-empty': 0.16; 'previously,': 0.16; 'reedy': 0.16; 'subject:between': 0.16; 'subject:random': 0.16; 'two.': 0.16; 'url:bugs': 0.17; 'wrote:': 0.18; '3.2': 0.18; 'int': 0.18; 'issue.': 0.19; 'written': 0.19; 'jan': 0.19; 'appears': 0.19; 'header:In-Reply-To:1': 0.22; 'changed': 0.23; 'expect': 0.25; 'module': 0.26; 'raise': 0.28; 'random': 0.28; 'effect': 0.28; 'producing': 0.28; 'affected': 0.29; 'version.': 0.29; 'pm,': 0.29; 'hash': 0.30; 'subject:?': 0.31; 'version': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.34; 'discussed': 0.34; 'steven': 0.34; 'identical': 0.34; 'integer': 0.34; 'try:': 0.34; 'probably': 0.35; 'to:addr:python-list': 0.35; 'url:python': 0.35; 'two': 0.36; 'received:org': 0.36; 'element': 0.37; 'similar': 0.37; 'using': 0.37; 'should': 0.38; 'url:org': 0.39; 'except': 0.39; 'change': 0.40; 'to:addr:python.org': 0.40; 'selection': 0.40; 'range': 0.61; 'power': 0.63; 'choose': 0.64; 'results': 0.64; 'kept': 0.68; 'policy.': 0.71; 'policy,': 0.77; 'bias': 0.84; 'uniform': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: difference between random module in python 2.6 and 3.2?
Date Mon, 06 Feb 2012 00:07:04 -0500
References <jgna4q$1t08$1@ns.felk.cvut.cz> <4f2f5081$0$29992$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <4f2f5081$0$29992$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5464.1328504850.27778.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328504850 news.xs4all.nl 6850 [2001:888:2000:d::a6]:42831
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19887

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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