summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 4642928..4efbb0a 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -96,11 +96,13 @@ class Random(_random.Random):
None or no argument seeds from current time or from an operating
system specific randomness source if available.
- 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.
+ For version 2 (the default), all of the bits are used if *a* is a str,
+ bytes, or bytearray. For version 1 (provided for reproducing random
+ sequences from older versions of Python), the algorithm for str and
+ bytes generates a narrower range of seeds.
+
"""
if a is None:
@@ -112,6 +114,13 @@ class Random(_random.Random):
import time
a = int(time.time() * 256) # use fractional seconds
+ if version == 1 and isinstance(a, (str, bytes)):
+ x = ord(a[0]) << 7 if a else 0
+ for c in a:
+ x = ((1000003 * x) ^ ord(c)) & 0xFFFFFFFFFFFFFFFF
+ x ^= len(a)
+ a = -2 if x == -1 else x
+
if version == 2:
if isinstance(a, (str, bytes, bytearray)):
if isinstance(a, str):
@@ -231,7 +240,7 @@ class Random(_random.Random):
while r >= n:
r = getrandbits(k)
return r
- # There's an overriden random() method but no new getrandbits() method,
+ # There's an overridden random() method but no new getrandbits() method,
# so we can only use random() from here.
if n >= maxsize:
_warn("Underlying random() generator does not supply \n"
@@ -687,7 +696,7 @@ def _test_generator(n, func, args):
print(round(t1-t0, 3), 'sec,', end=' ')
avg = total/n
stddev = _sqrt(sqsum/n - avg*avg)
- print('avg %g, stddev %g, min %g, max %g' % \
+ print('avg %g, stddev %g, min %g, max %g\n' % \
(avg, stddev, smallest, largest))