]> granicus.if.org Git - python/commitdiff
SF bug #778964: bad seed in python 2.3 random
authorRaymond Hettinger <python@rcn.com>
Sat, 9 Aug 2003 18:30:57 +0000 (18:30 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 9 Aug 2003 18:30:57 +0000 (18:30 +0000)
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.

Lib/random.py
Lib/test/test_random.py
Misc/NEWS

index 76dc41699a50fdf3d6f649d39b8d592acc4e2b1b..7932ac71e9d01c07c1940997ac19f1c7f28c929a 100644 (file)
@@ -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
 
index ea2243d1de91af5a0aca413a99477a7281e9ede3..c9103e8f87b77f424c6787ef467f0da0c095de5a 100644 (file)
@@ -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)
index 1de08e7249f641516619d3d5fd7481c3a456ddc2..93c7fb8b8d594cd63ac0f34486c14ac0c18d7c0e 100644 (file)
--- 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.