Commit 3081d59f authored by Raymond Hettinger's avatar Raymond Hettinger

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.
parent 39a682f5
...@@ -94,6 +94,9 @@ class Random(_random.Random): ...@@ -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 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) super(Random, self).seed(a)
self.gauss_next = None self.gauss_next = None
......
...@@ -20,7 +20,7 @@ class TestBasicOps(unittest.TestCase): ...@@ -20,7 +20,7 @@ class TestBasicOps(unittest.TestCase):
def test_autoseed(self): def test_autoseed(self):
self.gen.seed() self.gen.seed()
state1 = self.gen.getstate() state1 = self.gen.getstate()
time.sleep(1.1) time.sleep(0.1)
self.gen.seed() # diffent seeds at different times self.gen.seed() # diffent seeds at different times
state2 = self.gen.getstate() state2 = self.gen.getstate()
self.assertNotEqual(state1, state2) self.assertNotEqual(state1, state2)
......
...@@ -25,6 +25,10 @@ Extension modules ...@@ -25,6 +25,10 @@ Extension modules
Library 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 - itertools.izip() with no arguments now returns an empty iterator instead
of raising a TypeError exception. of raising a TypeError exception.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment