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