[Python-checkins] r84665 - in python/branches/release27-maint: Lib/random.py Lib/test/test_random.py Misc/NEWS
raymond.hettinger
python-checkins at python.org
Fri Sep 10 12:47:25 CEST 2010
Author: raymond.hettinger
Date: Fri Sep 10 12:47:22 2010
New Revision: 84665
Log:
Issue 9816: Random.jumpahead(n) didn't work well for small values of n.
Modified:
python/branches/release27-maint/Lib/random.py
python/branches/release27-maint/Lib/test/test_random.py
python/branches/release27-maint/Misc/NEWS
Modified: python/branches/release27-maint/Lib/random.py
==============================================================================
--- python/branches/release27-maint/Lib/random.py (original)
+++ python/branches/release27-maint/Lib/random.py Fri Sep 10 12:47:22 2010
@@ -46,6 +46,7 @@
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
from os import urandom as _urandom
from binascii import hexlify as _hexlify
+import hashlib as _hashlib
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
"randrange","shuffle","normalvariate","lognormvariate",
@@ -141,6 +142,18 @@
"Random.setstate() of version %s" %
(version, self.VERSION))
+ def jumpahead(self, n):
+ """Change the internal state to one that is likely far away
+ from the current state. This method will not be in Py3.x,
+ so it is better to simply reseed.
+ """
+ # The super.jumpahead() method uses shuffling to change state,
+ # so it needs a large and "interesting" n to work with. Here,
+ # we use hashing to create a large n for the shuffle.
+ s = repr(n) + repr(self.getstate())
+ n = int(_hashlib.new('sha512', s).hexdigest(), 16)
+ super(Random, self).jumpahead(n)
+
## ---- Methods below this point do not need to be overridden when
## ---- subclassing for the purpose of using a different core generator.
Modified: python/branches/release27-maint/Lib/test/test_random.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_random.py (original)
+++ python/branches/release27-maint/Lib/test/test_random.py Fri Sep 10 12:47:22 2010
@@ -55,8 +55,6 @@
with test_support.check_py3k_warnings(quiet=True):
self.assertRaises(TypeError, self.gen.jumpahead) # needs an arg
- self.assertRaises(TypeError, self.gen.jumpahead, "ick") # wrong type
- self.assertRaises(TypeError, self.gen.jumpahead, 2.3) # wrong type
self.assertRaises(TypeError, self.gen.jumpahead, 2, 3) # too many
def test_sample(self):
Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS (original)
+++ python/branches/release27-maint/Misc/NEWS Fri Sep 10 12:47:22 2010
@@ -43,6 +43,10 @@
Library
-------
+- Issue #9816: random.Random.jumpahead(n) did not produce a sufficiently
+ different internal state for small values of n. Fixed by salting the
+ value.
+
- Issue #9792: In case of connection failure, socket.create_connection()
would swallow the exception and raise a new one, making it impossible
to fetch the original errno, or to filter timeout errors. Now the
More information about the Python-checkins
mailing list