[Python-checkins] python/nondist/sandbox/twister random.py,1.9,1.10 test_random.py,1.11,1.12

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Sun, 29 Dec 2002 14:35:16 -0800


Update of /cvsroot/python/python/nondist/sandbox/twister
In directory sc8-pr-cvs1:/tmp/cvs-serv1610

Modified Files:
	random.py test_random.py 
Log Message:
Incorporated existing test_random.py.
Fixed bug where gauss variable did not get re-initialized by seeding.
Whitespace normalization.
Minor edits to comments.


Index: random.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/random.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** random.py	29 Dec 2002 06:59:36 -0000	1.9
--- random.py	29 Dec 2002 22:35:14 -0000	1.10
***************
*** 30,34 ****
  
  * The period is 2**19937-1.
! * It passes the Diehard battery of tests for randomness
  * Without a direct way to compute N steps forward, the
    semantics of jumpahead(n) are weakened to simply jump
--- 30,34 ----
  
  * The period is 2**19937-1.
! * It is one of the most extensively tested generators in existence
  * Without a direct way to compute N steps forward, the
    semantics of jumpahead(n) are weakened to simply jump
***************
*** 85,88 ****
--- 85,99 ----
  
          self.seed(x)
+         self.gauss_next = None
+ 
+     def seed(self, a=None):
+         """Initialize internal state from hashable object.
+ 
+         None or no argument seeds from current time.
+ 
+         If a is not None or an int or long, hash(a) is used instead.
+         """
+ 
+         CoreGenerator.seed(self, a)
          self.gauss_next = None
  

Index: test_random.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/test_random.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** test_random.py	29 Dec 2002 20:26:56 -0000	1.11
--- test_random.py	29 Dec 2002 22:35:14 -0000	1.12
***************
*** 66,69 ****
--- 66,87 ----
              self.failIf(None in uniq)
  
+     def test_gauss(self):
+         # Ensure that the seed() method initializes all the hidden state.  In
+         # particular, through 2.2.1 it failed to reset a piece of state used
+         # by (and only by) the .gauss() method.
+ 
+         for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
+             self.gen.seed(seed)
+             x1 = self.gen.random()
+             y1 = self.gen.gauss(0, 1)
+ 
+             self.gen.seed(seed)
+             x2 = self.gen.random()
+             y2 = self.gen.gauss(0, 1)
+ 
+             self.assertEqual(x1, x2)
+             self.assertEqual(y1, y2)
+ 
+ 
  class WichmannHill_TestBasicOps(TestBasicOps):
      gen = random.WichmannHill()
***************
*** 81,84 ****
--- 99,119 ----
          r2 = self.gen.random()
          self.assertEqual(r1, r2)
+ 
+     def test_gauss_with_whseed(self):
+         # Ensure that the seed() method initializes all the hidden state.  In
+         # particular, through 2.2.1 it failed to reset a piece of state used
+         # by (and only by) the .gauss() method.
+ 
+         for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
+             self.gen.whseed(seed)
+             x1 = self.gen.random()
+             y1 = self.gen.gauss(0, 1)
+ 
+             self.gen.whseed(seed)
+             x2 = self.gen.random()
+             y2 = self.gen.gauss(0, 1)
+ 
+             self.assertEqual(x1, x2)
+             self.assertEqual(y1, y2)
  
  class MersenneTwister_TestBasicOps(TestBasicOps):