]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
SF bug #778964: bad seed in python 2.3 random
authorRaymond Hettinger <python@rcn.com>
Sat, 9 Aug 2003 18:20:16 +0000 (18:20 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 9 Aug 2003 18:20:16 +0000 (18:20 +0000)
The default seed is time.time().
Multiplied by 256 before truncating so that fractional seconds are used.
This way, two consequetive calls to random.seed() are much more likely
to produce different sequences.

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

index 1a0b8f341e478cd88b98c10b31a2c1a6605288f9..b1d13c0dfb599e3ff9b9d533c8271ed8d0608db0 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 b27ba1939efb83cae2e0ff93bb1667ddeb38b860..d3d229d7f6535d6a398c8a7b9a7ef1883a94a53d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Extension modules
 Library
 -------
 
+- Bug #778964:  random.seed() now uses fractional seconds so that
+  rapid successive, seeding calls will produce different sequences.
+
 - Bug #777664: Add Tkconstants.HIDDEN.
 
 - Bug #781065: test_normalization is updated to the current