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: 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 Subject: Re: difference between random module in python 2.6 and 3.2? Date: Mon, 06 Feb 2012 00:07:04 -0500 References: <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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