summaryrefslogtreecommitdiff
path: root/Lib/random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-10 10:47:22 +0000
committerRaymond Hettinger <python@rcn.com>2010-09-10 10:47:22 +0000
commitffd2a4215a0bfe82f48ff71381bbfce8552f5f0c (patch)
tree74cfadca537a186e8564511bca62c81b694434c1 /Lib/random.py
parentd55ffdbee38ba9693a2d3346318ef51b5840ea65 (diff)
downloadcpython-git-ffd2a4215a0bfe82f48ff71381bbfce8552f5f0c.tar.gz
Issue 9816: Random.jumpahead(n) didn't work well for small values of n.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/random.py b/Lib/random.py
index f919718a6d..01e1420df6 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -46,6 +46,7 @@ from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
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 @@ class Random(_random.Random):
"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.