Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 4 | import random |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 5 | import time |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 6 | import pickle |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 7 | import warnings |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 8 | from math import log, exp, sqrt, pi |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 10 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 11 | class TestBasicOps(unittest.TestCase): |
| 12 | # Superclass with tests common to all generators. |
| 13 | # Subclasses must arrange for self.gen to retrieve the Random instance |
| 14 | # to be tested. |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 15 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 16 | def randomlist(self, n): |
| 17 | """Helper function to make a list of random numbers""" |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 18 | return [self.gen.random() for i in range(n)] |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 19 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 20 | def test_autoseed(self): |
| 21 | self.gen.seed() |
| 22 | state1 = self.gen.getstate() |
Raymond Hettinger | 3081d59 | 2003-08-09 18:30:57 +0000 | [diff] [blame] | 23 | time.sleep(0.1) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 24 | self.gen.seed() # diffent seeds at different times |
| 25 | state2 = self.gen.getstate() |
| 26 | self.assertNotEqual(state1, state2) |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 27 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 28 | def test_saverestore(self): |
| 29 | N = 1000 |
| 30 | self.gen.seed() |
| 31 | state = self.gen.getstate() |
| 32 | randseq = self.randomlist(N) |
| 33 | self.gen.setstate(state) # should regenerate the same sequence |
| 34 | self.assertEqual(randseq, self.randomlist(N)) |
| 35 | |
| 36 | def test_seedargs(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 37 | for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20), |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 38 | 3.14, 1+2j, 'a', tuple('abc')]: |
| 39 | self.gen.seed(arg) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 40 | for arg in [list(range(3)), dict(one=1)]: |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 41 | self.assertRaises(TypeError, self.gen.seed, arg) |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 42 | self.assertRaises(TypeError, self.gen.seed, 1, 2) |
| 43 | self.assertRaises(TypeError, type(self.gen), []) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 44 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 45 | def test_sample(self): |
| 46 | # For the entire allowable range of 0 <= k <= N, validate that |
| 47 | # the sample is of the correct length and contains only unique items |
| 48 | N = 100 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 49 | population = range(N) |
| 50 | for k in range(N+1): |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 51 | s = self.gen.sample(population, k) |
| 52 | self.assertEqual(len(s), k) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 53 | uniq = set(s) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 54 | self.assertEqual(len(uniq), k) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 55 | self.failUnless(uniq <= set(population)) |
Raymond Hettinger | 8ec7881 | 2003-01-04 05:55:11 +0000 | [diff] [blame] | 56 | self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 57 | |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 58 | def test_sample_distribution(self): |
| 59 | # For the entire allowable range of 0 <= k <= N, validate that |
| 60 | # sample generates all possible permutations |
| 61 | n = 5 |
| 62 | pop = range(n) |
| 63 | trials = 10000 # large num prevents false negatives without slowing normal case |
| 64 | def factorial(n): |
Guido van Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 65 | if n == 0: |
| 66 | return 1 |
| 67 | return n * factorial(n - 1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 68 | for k in range(n): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 69 | expected = factorial(n) // factorial(n-k) |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 70 | perms = {} |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 71 | for i in range(trials): |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 72 | perms[tuple(self.gen.sample(pop, k))] = None |
| 73 | if len(perms) == expected: |
| 74 | break |
| 75 | else: |
| 76 | self.fail() |
| 77 | |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 78 | def test_sample_inputs(self): |
| 79 | # SF bug #801342 -- population can be any iterable defining __len__() |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 80 | self.gen.sample(set(range(20)), 2) |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 81 | self.gen.sample(range(20), 2) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 82 | self.gen.sample(range(20), 2) |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 83 | self.gen.sample(str('abcdefghijklmnopqrst'), 2) |
| 84 | self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) |
| 85 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | def test_sample_on_dicts(self): |
Raymond Hettinger | 1acde19 | 2008-01-14 01:00:53 +0000 | [diff] [blame] | 87 | self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 89 | def test_gauss(self): |
| 90 | # Ensure that the seed() method initializes all the hidden state. In |
| 91 | # particular, through 2.2.1 it failed to reset a piece of state used |
| 92 | # by (and only by) the .gauss() method. |
| 93 | |
| 94 | for seed in 1, 12, 123, 1234, 12345, 123456, 654321: |
| 95 | self.gen.seed(seed) |
| 96 | x1 = self.gen.random() |
| 97 | y1 = self.gen.gauss(0, 1) |
| 98 | |
| 99 | self.gen.seed(seed) |
| 100 | x2 = self.gen.random() |
| 101 | y2 = self.gen.gauss(0, 1) |
| 102 | |
| 103 | self.assertEqual(x1, x2) |
| 104 | self.assertEqual(y1, y2) |
| 105 | |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 106 | def test_pickling(self): |
| 107 | state = pickle.dumps(self.gen) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 108 | origseq = [self.gen.random() for i in range(10)] |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 109 | newgen = pickle.loads(state) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 110 | restoredseq = [newgen.random() for i in range(10)] |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 111 | self.assertEqual(origseq, restoredseq) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 112 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 113 | def test_bug_1727780(self): |
| 114 | # verify that version-2-pickles can be loaded |
| 115 | # fine, whether they are created on 32-bit or 64-bit |
| 116 | # platforms, and that version-3-pickles load fine. |
| 117 | files = [("randv2_32.pck", 780), |
| 118 | ("randv2_64.pck", 866), |
| 119 | ("randv3.pck", 343)] |
| 120 | for file, value in files: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 121 | f = open(support.findfile(file),"rb") |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 122 | r = pickle.load(f) |
| 123 | f.close() |
| 124 | self.assertEqual(r.randrange(1000), value) |
| 125 | |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 126 | class SystemRandom_TestBasicOps(TestBasicOps): |
| 127 | gen = random.SystemRandom() |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 128 | |
| 129 | def test_autoseed(self): |
| 130 | # Doesn't need to do anything except not fail |
| 131 | self.gen.seed() |
| 132 | |
| 133 | def test_saverestore(self): |
| 134 | self.assertRaises(NotImplementedError, self.gen.getstate) |
| 135 | self.assertRaises(NotImplementedError, self.gen.setstate, None) |
| 136 | |
| 137 | def test_seedargs(self): |
| 138 | # Doesn't need to do anything except not fail |
| 139 | self.gen.seed(100) |
| 140 | |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 141 | def test_gauss(self): |
| 142 | self.gen.gauss_next = None |
| 143 | self.gen.seed(100) |
| 144 | self.assertEqual(self.gen.gauss_next, None) |
| 145 | |
| 146 | def test_pickling(self): |
| 147 | self.assertRaises(NotImplementedError, pickle.dumps, self.gen) |
| 148 | |
| 149 | def test_53_bits_per_float(self): |
| 150 | # This should pass whenever a C double has 53 bit precision. |
| 151 | span = 2 ** 53 |
| 152 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 153 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 154 | cum |= int(self.gen.random() * span) |
| 155 | self.assertEqual(cum, span-1) |
| 156 | |
| 157 | def test_bigrand(self): |
| 158 | # The randrange routine should build-up the required number of bits |
| 159 | # in stages so that all bit positions are active. |
| 160 | span = 2 ** 500 |
| 161 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 162 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 163 | r = self.gen.randrange(span) |
| 164 | self.assert_(0 <= r < span) |
| 165 | cum |= r |
| 166 | self.assertEqual(cum, span-1) |
| 167 | |
| 168 | def test_bigrand_ranges(self): |
| 169 | for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
| 170 | start = self.gen.randrange(2 ** i) |
| 171 | stop = self.gen.randrange(2 ** (i-2)) |
| 172 | if stop <= start: |
| 173 | return |
| 174 | self.assert_(start <= self.gen.randrange(start, stop) < stop) |
| 175 | |
| 176 | def test_rangelimits(self): |
| 177 | for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
| 178 | self.assertEqual(set(range(start,stop)), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 179 | set([self.gen.randrange(start,stop) for i in range(100)])) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 180 | |
| 181 | def test_genrandbits(self): |
| 182 | # Verify ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 183 | for k in range(1, 1000): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 184 | self.assert_(0 <= self.gen.getrandbits(k) < 2**k) |
| 185 | |
| 186 | # Verify all bits active |
| 187 | getbits = self.gen.getrandbits |
| 188 | for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
| 189 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 190 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 191 | cum |= getbits(span) |
| 192 | self.assertEqual(cum, 2**span-1) |
| 193 | |
| 194 | # Verify argument checking |
| 195 | self.assertRaises(TypeError, self.gen.getrandbits) |
| 196 | self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
| 197 | self.assertRaises(ValueError, self.gen.getrandbits, 0) |
| 198 | self.assertRaises(ValueError, self.gen.getrandbits, -1) |
| 199 | self.assertRaises(TypeError, self.gen.getrandbits, 10.1) |
| 200 | |
| 201 | def test_randbelow_logic(self, _log=log, int=int): |
| 202 | # check bitcount transition points: 2**i and 2**(i+1)-1 |
| 203 | # show that: k = int(1.001 + _log(n, 2)) |
| 204 | # is equal to or one greater than the number of bits in n |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 205 | for i in range(1, 1000): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 206 | n = 1 << i # check an exact power of two |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 207 | numbits = i+1 |
| 208 | k = int(1.00001 + _log(n, 2)) |
| 209 | self.assertEqual(k, numbits) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 210 | self.assertEqual(n, 2**(k-1)) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 211 | |
| 212 | n += n - 1 # check 1 below the next power of two |
| 213 | k = int(1.00001 + _log(n, 2)) |
| 214 | self.assert_(k in [numbits, numbits+1]) |
| 215 | self.assert_(2**k > n > 2**(k-2)) |
| 216 | |
| 217 | n -= n >> 15 # check a little farther below the next power of two |
| 218 | k = int(1.00001 + _log(n, 2)) |
| 219 | self.assertEqual(k, numbits) # note the stronger assertion |
| 220 | self.assert_(2**k > n > 2**(k-1)) # note the stronger assertion |
| 221 | |
| 222 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 223 | class MersenneTwister_TestBasicOps(TestBasicOps): |
| 224 | gen = random.Random() |
| 225 | |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 226 | def test_setstate_first_arg(self): |
| 227 | self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) |
| 228 | |
| 229 | def test_setstate_middle_arg(self): |
| 230 | # Wrong type, s/b tuple |
| 231 | self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) |
| 232 | # Wrong length, s/b 625 |
| 233 | self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) |
| 234 | # Wrong type, s/b tuple of 625 ints |
| 235 | self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) |
| 236 | # Last element s/b an int also |
| 237 | self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) |
| 238 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 239 | def test_referenceImplementation(self): |
| 240 | # Compare the python implementation with results from the original |
| 241 | # code. Create 2000 53-bit precision random floats. Compare only |
| 242 | # the last ten entries to show that the independent implementations |
| 243 | # are tracking. Here is the main() function needed to create the |
| 244 | # list of expected random numbers: |
| 245 | # void main(void){ |
| 246 | # int i; |
| 247 | # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; |
| 248 | # init_by_array(init, length); |
| 249 | # for (i=0; i<2000; i++) { |
| 250 | # printf("%.15f ", genrand_res53()); |
| 251 | # if (i%5==4) printf("\n"); |
| 252 | # } |
| 253 | # } |
| 254 | expected = [0.45839803073713259, |
| 255 | 0.86057815201978782, |
| 256 | 0.92848331726782152, |
| 257 | 0.35932681119782461, |
| 258 | 0.081823493762449573, |
| 259 | 0.14332226470169329, |
| 260 | 0.084297823823520024, |
| 261 | 0.53814864671831453, |
| 262 | 0.089215024911993401, |
| 263 | 0.78486196105372907] |
| 264 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 265 | self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 266 | actual = self.randomlist(2000)[-10:] |
| 267 | for a, e in zip(actual, expected): |
| 268 | self.assertAlmostEqual(a,e,places=14) |
| 269 | |
| 270 | def test_strong_reference_implementation(self): |
| 271 | # Like test_referenceImplementation, but checks for exact bit-level |
| 272 | # equality. This should pass on any box where C double contains |
| 273 | # at least 53 bits of precision (the underlying algorithm suffers |
| 274 | # no rounding errors -- all results are exact). |
| 275 | from math import ldexp |
| 276 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 277 | expected = [0x0eab3258d2231f, |
| 278 | 0x1b89db315277a5, |
| 279 | 0x1db622a5518016, |
| 280 | 0x0b7f9af0d575bf, |
| 281 | 0x029e4c4db82240, |
| 282 | 0x04961892f5d673, |
| 283 | 0x02b291598e4589, |
| 284 | 0x11388382c15694, |
| 285 | 0x02dad977c9e1fe, |
| 286 | 0x191d96d4d334c6] |
| 287 | self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 288 | actual = self.randomlist(2000)[-10:] |
| 289 | for a, e in zip(actual, expected): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 290 | self.assertEqual(int(ldexp(a, 53)), e) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 291 | |
| 292 | def test_long_seed(self): |
| 293 | # This is most interesting to run in debug mode, just to make sure |
| 294 | # nothing blows up. Under the covers, a dynamically resized array |
| 295 | # is allocated, consuming space proportional to the number of bits |
| 296 | # in the seed. Unfortunately, that's a quadratic-time algorithm, |
| 297 | # so don't make this horribly big. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 298 | seed = (1 << (10000 * 8)) - 1 # about 10K bytes |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 299 | self.gen.seed(seed) |
| 300 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 301 | def test_53_bits_per_float(self): |
| 302 | # This should pass whenever a C double has 53 bit precision. |
| 303 | span = 2 ** 53 |
| 304 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 305 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 306 | cum |= int(self.gen.random() * span) |
| 307 | self.assertEqual(cum, span-1) |
| 308 | |
| 309 | def test_bigrand(self): |
| 310 | # The randrange routine should build-up the required number of bits |
| 311 | # in stages so that all bit positions are active. |
| 312 | span = 2 ** 500 |
| 313 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 314 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 315 | r = self.gen.randrange(span) |
| 316 | self.assert_(0 <= r < span) |
| 317 | cum |= r |
| 318 | self.assertEqual(cum, span-1) |
| 319 | |
| 320 | def test_bigrand_ranges(self): |
| 321 | for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
| 322 | start = self.gen.randrange(2 ** i) |
| 323 | stop = self.gen.randrange(2 ** (i-2)) |
| 324 | if stop <= start: |
| 325 | return |
| 326 | self.assert_(start <= self.gen.randrange(start, stop) < stop) |
| 327 | |
| 328 | def test_rangelimits(self): |
| 329 | for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 330 | self.assertEqual(set(range(start,stop)), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 331 | set([self.gen.randrange(start,stop) for i in range(100)])) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 332 | |
| 333 | def test_genrandbits(self): |
| 334 | # Verify cross-platform repeatability |
| 335 | self.gen.seed(1234567) |
| 336 | self.assertEqual(self.gen.getrandbits(100), |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 337 | 97904845777343510404718956115) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 338 | # Verify ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 339 | for k in range(1, 1000): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 340 | self.assert_(0 <= self.gen.getrandbits(k) < 2**k) |
| 341 | |
| 342 | # Verify all bits active |
| 343 | getbits = self.gen.getrandbits |
| 344 | for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
| 345 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 346 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 347 | cum |= getbits(span) |
| 348 | self.assertEqual(cum, 2**span-1) |
| 349 | |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 350 | # Verify argument checking |
| 351 | self.assertRaises(TypeError, self.gen.getrandbits) |
| 352 | self.assertRaises(TypeError, self.gen.getrandbits, 'a') |
| 353 | self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
| 354 | self.assertRaises(ValueError, self.gen.getrandbits, 0) |
| 355 | self.assertRaises(ValueError, self.gen.getrandbits, -1) |
| 356 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 357 | def test_randbelow_logic(self, _log=log, int=int): |
| 358 | # check bitcount transition points: 2**i and 2**(i+1)-1 |
| 359 | # show that: k = int(1.001 + _log(n, 2)) |
| 360 | # is equal to or one greater than the number of bits in n |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 361 | for i in range(1, 1000): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 362 | n = 1 << i # check an exact power of two |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 363 | numbits = i+1 |
| 364 | k = int(1.00001 + _log(n, 2)) |
| 365 | self.assertEqual(k, numbits) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 366 | self.assertEqual(n, 2**(k-1)) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 367 | |
| 368 | n += n - 1 # check 1 below the next power of two |
| 369 | k = int(1.00001 + _log(n, 2)) |
| 370 | self.assert_(k in [numbits, numbits+1]) |
| 371 | self.assert_(2**k > n > 2**(k-2)) |
| 372 | |
| 373 | n -= n >> 15 # check a little farther below the next power of two |
| 374 | k = int(1.00001 + _log(n, 2)) |
| 375 | self.assertEqual(k, numbits) # note the stronger assertion |
| 376 | self.assert_(2**k > n > 2**(k-1)) # note the stronger assertion |
| 377 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 378 | def test_randrange_bug_1590891(self): |
| 379 | start = 1000000000000 |
| 380 | stop = -100000000000000000000 |
| 381 | step = -200 |
| 382 | x = self.gen.randrange(start, stop, step) |
| 383 | self.assert_(stop < x <= start) |
| 384 | self.assertEqual((x+stop)%step, 0) |
| 385 | |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 386 | _gammacoeff = (0.9999999999995183, 676.5203681218835, -1259.139216722289, |
| 387 | 771.3234287757674, -176.6150291498386, 12.50734324009056, |
| 388 | -0.1385710331296526, 0.9934937113930748e-05, 0.1659470187408462e-06) |
| 389 | |
| 390 | def gamma(z, cof=_gammacoeff, g=7): |
| 391 | z -= 1.0 |
| 392 | sum = cof[0] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 393 | for i in range(1,len(cof)): |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 394 | sum += cof[i] / (z+i) |
| 395 | z += 0.5 |
| 396 | return (z+g)**z / exp(z+g) * sqrt(2*pi) * sum |
| 397 | |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 398 | class TestDistributions(unittest.TestCase): |
| 399 | def test_zeroinputs(self): |
| 400 | # Verify that distributions can handle a series of zero inputs' |
| 401 | g = random.Random() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 402 | x = [g.random() for i in range(50)] + [0.0]*5 |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 403 | g.random = x[:].pop; g.uniform(1,10) |
| 404 | g.random = x[:].pop; g.paretovariate(1.0) |
| 405 | g.random = x[:].pop; g.expovariate(1.0) |
| 406 | g.random = x[:].pop; g.weibullvariate(1.0, 1.0) |
| 407 | g.random = x[:].pop; g.normalvariate(0.0, 1.0) |
| 408 | g.random = x[:].pop; g.gauss(0.0, 1.0) |
| 409 | g.random = x[:].pop; g.lognormvariate(0.0, 1.0) |
| 410 | g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) |
| 411 | g.random = x[:].pop; g.gammavariate(0.01, 1.0) |
| 412 | g.random = x[:].pop; g.gammavariate(1.0, 1.0) |
| 413 | g.random = x[:].pop; g.gammavariate(200.0, 1.0) |
| 414 | g.random = x[:].pop; g.betavariate(3.0, 3.0) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 415 | g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 416 | |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 417 | def test_avg_std(self): |
| 418 | # Use integration to test distribution average and standard deviation. |
| 419 | # Only works for distributions which do not consume variates in pairs |
| 420 | g = random.Random() |
| 421 | N = 5000 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 422 | x = [i/float(N) for i in range(1,N)] |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 423 | for variate, args, mu, sigmasqrd in [ |
| 424 | (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 425 | (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0), |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 426 | (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), |
| 427 | (g.paretovariate, (5.0,), 5.0/(5.0-1), |
| 428 | 5.0/((5.0-1)**2*(5.0-2))), |
| 429 | (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), |
| 430 | gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: |
| 431 | g.random = x[:].pop |
| 432 | y = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 433 | for i in range(len(x)): |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 434 | try: |
| 435 | y.append(variate(*args)) |
| 436 | except IndexError: |
| 437 | pass |
| 438 | s1 = s2 = 0 |
| 439 | for e in y: |
| 440 | s1 += e |
| 441 | s2 += (e - mu) ** 2 |
| 442 | N = len(y) |
Jeffrey Yasskin | 48952d3 | 2007-09-07 15:45:05 +0000 | [diff] [blame] | 443 | self.assertAlmostEqual(s1/N, mu, places=2) |
| 444 | self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2) |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 445 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 446 | class TestModule(unittest.TestCase): |
| 447 | def testMagicConstants(self): |
| 448 | self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) |
| 449 | self.assertAlmostEqual(random.TWOPI, 6.28318530718) |
| 450 | self.assertAlmostEqual(random.LOG4, 1.38629436111989) |
| 451 | self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) |
| 452 | |
| 453 | def test__all__(self): |
| 454 | # tests validity but not completeness of the __all__ list |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 455 | self.failUnless(set(random.__all__) <= set(dir(random))) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 456 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 457 | def test_random_subclass_with_kwargs(self): |
| 458 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 459 | class Subclass(random.Random): |
| 460 | def __init__(self, newarg=None): |
| 461 | random.Random.__init__(self) |
| 462 | Subclass(newarg=1) |
| 463 | |
| 464 | |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 465 | def test_main(verbose=None): |
Raymond Hettinger | 28de64f | 2008-01-13 23:40:30 +0000 | [diff] [blame] | 466 | testclasses = [MersenneTwister_TestBasicOps, |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 467 | TestDistributions, |
Raymond Hettinger | b871763 | 2004-09-04 20:13:29 +0000 | [diff] [blame] | 468 | TestModule] |
| 469 | |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 470 | try: |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 471 | random.SystemRandom().random() |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 472 | except NotImplementedError: |
| 473 | pass |
| 474 | else: |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 475 | testclasses.append(SystemRandom_TestBasicOps) |
Raymond Hettinger | b871763 | 2004-09-04 20:13:29 +0000 | [diff] [blame] | 476 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 477 | support.run_unittest(*testclasses) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 478 | |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 479 | # verify reference counting |
| 480 | import sys |
| 481 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 320a1b0 | 2003-05-02 22:44:59 +0000 | [diff] [blame] | 482 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 483 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 484 | support.run_unittest(*testclasses) |
Raymond Hettinger | 320a1b0 | 2003-05-02 22:44:59 +0000 | [diff] [blame] | 485 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 486 | print(counts) |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 487 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 488 | if __name__ == "__main__": |
Raymond Hettinger | 105b084 | 2003-02-04 05:47:30 +0000 | [diff] [blame] | 489 | test_main(verbose=True) |