summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-07 00:38:15 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-09-07 00:38:15 (GMT)
commitf763a728adf45926bd75fc6a499eefa4d845b683 (patch)
tree34208612863bd29e798d45a08872364b5456c233 /Lib/random.py
parent435cb0f23304d57b0cafb2a4449a41150f7fffa9 (diff)
downloadcpython-f763a728adf45926bd75fc6a499eefa4d845b683.zip
cpython-f763a728adf45926bd75fc6a499eefa4d845b683.tar.gz
cpython-f763a728adf45926bd75fc6a499eefa4d845b683.tar.bz2
Document which part of the random module module are guaranteed.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 592e4b8..4ff65ab 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -91,13 +91,17 @@ class Random(_random.Random):
self.seed(x)
self.gauss_next = None
- def seed(self, a=None):
+ def seed(self, a=None, version=2):
"""Initialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
- If a is not None or an int, hash(a) is used instead.
+ For version 2 (the default), all of the bits are used if a is a str,
+ bytes, or bytearray. For version 1, the hash() of a is used instead.
+
+ If a is an int, all bits are used.
+
"""
if a is None:
@@ -107,6 +111,11 @@ class Random(_random.Random):
import time
a = int(time.time() * 256) # use fractional seconds
+ if version == 2 and isinstance(a, (str, bytes, bytearray)):
+ if isinstance(a, str):
+ a = a.encode("utf8")
+ a = int(_hexlify(a), 16)
+
super().seed(a)
self.gauss_next = None