blob: d0a2a15599001b002c6718693881110527d90147 [file] [log] [blame]
Raymond Hettinger40f62172002-12-29 23:03:38 +00001#!/usr/bin/env python
2
3import unittest
Tim Peters46c04e12002-05-05 20:40:00 +00004import random
Raymond Hettinger40f62172002-12-29 23:03:38 +00005import time
6from test import test_support
Tim Peters46c04e12002-05-05 20:40:00 +00007
Raymond Hettinger40f62172002-12-29 23:03:38 +00008class TestBasicOps(unittest.TestCase):
9 # Superclass with tests common to all generators.
10 # Subclasses must arrange for self.gen to retrieve the Random instance
11 # to be tested.
Tim Peters46c04e12002-05-05 20:40:00 +000012
Raymond Hettinger40f62172002-12-29 23:03:38 +000013 def randomlist(self, n):
14 """Helper function to make a list of random numbers"""
15 return [self.gen.random() for i in xrange(n)]
Tim Peters46c04e12002-05-05 20:40:00 +000016
Raymond Hettinger40f62172002-12-29 23:03:38 +000017 def test_autoseed(self):
18 self.gen.seed()
19 state1 = self.gen.getstate()
20 time.sleep(1)
21 self.gen.seed() # diffent seeds at different times
22 state2 = self.gen.getstate()
23 self.assertNotEqual(state1, state2)
Tim Peters46c04e12002-05-05 20:40:00 +000024
Raymond Hettinger40f62172002-12-29 23:03:38 +000025 def test_saverestore(self):
26 N = 1000
27 self.gen.seed()
28 state = self.gen.getstate()
29 randseq = self.randomlist(N)
30 self.gen.setstate(state) # should regenerate the same sequence
31 self.assertEqual(randseq, self.randomlist(N))
32
33 def test_seedargs(self):
34 for arg in [None, 0, 0L, 1, 1L, -1, -1L, 10**20, -(10**20),
35 3.14, 1+2j, 'a', tuple('abc')]:
36 self.gen.seed(arg)
37 for arg in [range(3), dict(one=1)]:
38 self.assertRaises(TypeError, self.gen.seed, arg)
39
40 def test_jumpahead(self):
41 self.gen.seed()
42 state1 = self.gen.getstate()
43 self.gen.jumpahead(100)
44 state2 = self.gen.getstate() # s/b distinct from state1
45 self.assertNotEqual(state1, state2)
46 self.gen.jumpahead(100)
47 state3 = self.gen.getstate() # s/b distinct from state2
48 self.assertNotEqual(state2, state3)
49
50 self.assertRaises(TypeError, self.gen.jumpahead) # needs an arg
51 self.assertRaises(TypeError, self.gen.jumpahead, "ick") # wrong type
52 self.assertRaises(TypeError, self.gen.jumpahead, 2.3) # wrong type
53 self.assertRaises(TypeError, self.gen.jumpahead, 2, 3) # too many
54
55 def test_sample(self):
56 # For the entire allowable range of 0 <= k <= N, validate that
57 # the sample is of the correct length and contains only unique items
58 N = 100
59 population = xrange(N)
60 for k in xrange(N+1):
61 s = self.gen.sample(population, k)
62 self.assertEqual(len(s), k)
63 uniq = dict.fromkeys(s)
64 self.assertEqual(len(uniq), k)
65 self.failIf(None in uniq)
66
67 def test_gauss(self):
68 # Ensure that the seed() method initializes all the hidden state. In
69 # particular, through 2.2.1 it failed to reset a piece of state used
70 # by (and only by) the .gauss() method.
71
72 for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
73 self.gen.seed(seed)
74 x1 = self.gen.random()
75 y1 = self.gen.gauss(0, 1)
76
77 self.gen.seed(seed)
78 x2 = self.gen.random()
79 y2 = self.gen.gauss(0, 1)
80
81 self.assertEqual(x1, x2)
82 self.assertEqual(y1, y2)
83
84
85class WichmannHill_TestBasicOps(TestBasicOps):
86 gen = random.WichmannHill()
87
88 def test_strong_jumpahead(self):
89 # tests that jumpahead(n) semantics correspond to n calls to random()
90 N = 1000
91 s = self.gen.getstate()
92 self.gen.jumpahead(N)
93 r1 = self.gen.random()
94 # now do it the slow way
95 self.gen.setstate(s)
96 for i in xrange(N):
97 self.gen.random()
98 r2 = self.gen.random()
99 self.assertEqual(r1, r2)
100
101 def test_gauss_with_whseed(self):
102 # Ensure that the seed() method initializes all the hidden state. In
103 # particular, through 2.2.1 it failed to reset a piece of state used
104 # by (and only by) the .gauss() method.
105
106 for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
107 self.gen.whseed(seed)
108 x1 = self.gen.random()
109 y1 = self.gen.gauss(0, 1)
110
111 self.gen.whseed(seed)
112 x2 = self.gen.random()
113 y2 = self.gen.gauss(0, 1)
114
115 self.assertEqual(x1, x2)
116 self.assertEqual(y1, y2)
117
118class MersenneTwister_TestBasicOps(TestBasicOps):
119 gen = random.Random()
120
121 def test_referenceImplementation(self):
122 # Compare the python implementation with results from the original
123 # code. Create 2000 53-bit precision random floats. Compare only
124 # the last ten entries to show that the independent implementations
125 # are tracking. Here is the main() function needed to create the
126 # list of expected random numbers:
127 # void main(void){
128 # int i;
129 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
130 # init_by_array(init, length);
131 # for (i=0; i<2000; i++) {
132 # printf("%.15f ", genrand_res53());
133 # if (i%5==4) printf("\n");
134 # }
135 # }
136 expected = [0.45839803073713259,
137 0.86057815201978782,
138 0.92848331726782152,
139 0.35932681119782461,
140 0.081823493762449573,
141 0.14332226470169329,
142 0.084297823823520024,
143 0.53814864671831453,
144 0.089215024911993401,
145 0.78486196105372907]
146
147 self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96))
148 actual = self.randomlist(2000)[-10:]
149 for a, e in zip(actual, expected):
150 self.assertAlmostEqual(a,e,places=14)
151
152 def test_strong_reference_implementation(self):
153 # Like test_referenceImplementation, but checks for exact bit-level
154 # equality. This should pass on any box where C double contains
155 # at least 53 bits of precision (the underlying algorithm suffers
156 # no rounding errors -- all results are exact).
157 from math import ldexp
158
159 expected = [0x0eab3258d2231fL,
160 0x1b89db315277a5L,
161 0x1db622a5518016L,
162 0x0b7f9af0d575bfL,
163 0x029e4c4db82240L,
164 0x04961892f5d673L,
165 0x02b291598e4589L,
166 0x11388382c15694L,
167 0x02dad977c9e1feL,
168 0x191d96d4d334c6L]
169
170 self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96))
171 actual = self.randomlist(2000)[-10:]
172 for a, e in zip(actual, expected):
173 self.assertEqual(long(ldexp(a, 53)), e)
174
175 def test_long_seed(self):
176 # This is most interesting to run in debug mode, just to make sure
177 # nothing blows up. Under the covers, a dynamically resized array
178 # is allocated, consuming space proportional to the number of bits
179 # in the seed. Unfortunately, that's a quadratic-time algorithm,
180 # so don't make this horribly big.
181 seed = (1L << (10000 * 8)) - 1 # about 10K bytes
182 self.gen.seed(seed)
183
184class TestModule(unittest.TestCase):
185 def testMagicConstants(self):
186 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
187 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
188 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
189 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
190
191 def test__all__(self):
192 # tests validity but not completeness of the __all__ list
193 defined = dict.fromkeys(dir(random))
194 for entry in random.__all__:
195 self.failUnless(entry in defined)
196
197def test_main():
198 suite = unittest.TestSuite()
199 for testclass in (WichmannHill_TestBasicOps,
200 MersenneTwister_TestBasicOps,
201 TestModule):
202 suite.addTest(unittest.makeSuite(testclass))
203 test_support.run_suite(suite)
204
205if __name__ == "__main__":
206 test_main()