summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-08-09 18:30:57 +0000
committerRaymond Hettinger <python@rcn.com>2003-08-09 18:30:57 +0000
commit3081d59f920229b26293c7a3ee3f1a9da0da53e9 (patch)
tree08061aabfcbf2b426df2b79277079411778d5e8d
parent39a682f5f223fb988ba634ba74e20b2b490cb333 (diff)
downloadcpython-git-3081d59f920229b26293c7a3ee3f1a9da0da53e9.tar.gz
SF bug #778964: bad seed in python 2.3 random
The default seed is time.time(). Multiplied by 256 before truncating so that fractional seconds are used. This way, two successive calls to random.seed() are much more likely to produce different sequences.
-rw-r--r--Lib/random.py3
-rw-r--r--Lib/test/test_random.py2
-rw-r--r--Misc/NEWS4
3 files changed, 8 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 76dc41699a..7932ac71e9 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -94,6 +94,9 @@ class Random(_random.Random):
If a is not None or an int or long, hash(a) is used instead.
"""
+ if a is None:
+ import time
+ a = long(time.time() * 256) # use fractional seconds
super(Random, self).seed(a)
self.gauss_next = None
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index ea2243d1de..c9103e8f87 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -20,7 +20,7 @@ class TestBasicOps(unittest.TestCase):
def test_autoseed(self):
self.gen.seed()
state1 = self.gen.getstate()
- time.sleep(1.1)
+ time.sleep(0.1)
self.gen.seed() # diffent seeds at different times
state2 = self.gen.getstate()
self.assertNotEqual(state1, state2)
diff --git a/Misc/NEWS b/Misc/NEWS
index 1de08e7249..93c7fb8b8d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,10 @@ Extension modules
Library
-------
+- random.seed() with no arguments or None uses time.time() as a default
+ seed. Modified to match Py2.2 behavior and use fractional seconds so
+ that successive runs are more likely to produce different sequences.
+
- itertools.izip() with no arguments now returns an empty iterator instead
of raising a TypeError exception.