blob: e80ed17a8cb6bc3d1972d60541a6a5de8fb638e6 [file] [log] [blame]
Raymond Hettinger40f62172002-12-29 23:03:38 +00001import unittest
R David Murraye3e1c172013-04-02 12:47:23 -04002import unittest.mock
Tim Peters46c04e12002-05-05 20:40:00 +00003import random
Raymond Hettinger40f62172002-12-29 23:03:38 +00004import time
Raymond Hettinger5f078ff2003-06-24 20:29:04 +00005import pickle
Raymond Hettinger2f726e92003-10-05 09:09:15 +00006import warnings
R David Murraye3e1c172013-04-02 12:47:23 -04007from functools import partial
Georg Brandl1b37e872010-03-14 10:45:50 +00008from math import log, exp, pi, fsum, sin
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Tim Peters46c04e12002-05-05 20:40:00 +000010
Ezio Melotti3e4a98b2013-04-19 05:45:27 +030011class TestBasicOps:
Raymond Hettinger40f62172002-12-29 23:03:38 +000012 # Superclass with tests common to all generators.
13 # Subclasses must arrange for self.gen to retrieve the Random instance
14 # to be tested.
Tim Peters46c04e12002-05-05 20:40:00 +000015
Raymond Hettinger40f62172002-12-29 23:03:38 +000016 def randomlist(self, n):
17 """Helper function to make a list of random numbers"""
Guido van Rossum805365e2007-05-07 22:24:25 +000018 return [self.gen.random() for i in range(n)]
Tim Peters46c04e12002-05-05 20:40:00 +000019
Raymond Hettinger40f62172002-12-29 23:03:38 +000020 def test_autoseed(self):
21 self.gen.seed()
22 state1 = self.gen.getstate()
Raymond Hettinger3081d592003-08-09 18:30:57 +000023 time.sleep(0.1)
Raymond Hettinger40f62172002-12-29 23:03:38 +000024 self.gen.seed() # diffent seeds at different times
25 state2 = self.gen.getstate()
26 self.assertNotEqual(state1, state2)
Tim Peters46c04e12002-05-05 20:40:00 +000027
Raymond Hettinger40f62172002-12-29 23:03:38 +000028 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 Dickinson95aeae02012-06-24 11:05:30 +010037 # Seed value with a negative hash.
38 class MySeed(object):
39 def __hash__(self):
40 return -1729
Guido van Rossume2a383d2007-01-15 16:59:06 +000041 for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20),
Mark Dickinson95aeae02012-06-24 11:05:30 +010042 3.14, 1+2j, 'a', tuple('abc'), MySeed()]:
Raymond Hettinger40f62172002-12-29 23:03:38 +000043 self.gen.seed(arg)
Guido van Rossum805365e2007-05-07 22:24:25 +000044 for arg in [list(range(3)), dict(one=1)]:
Raymond Hettinger40f62172002-12-29 23:03:38 +000045 self.assertRaises(TypeError, self.gen.seed, arg)
Raymond Hettingerf763a722010-09-07 00:38:15 +000046 self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
Raymond Hettinger58335872004-07-09 14:26:18 +000047 self.assertRaises(TypeError, type(self.gen), [])
Raymond Hettinger40f62172002-12-29 23:03:38 +000048
R David Murraye3e1c172013-04-02 12:47:23 -040049 @unittest.mock.patch('random._urandom') # os.urandom
50 def test_seed_when_randomness_source_not_found(self, urandom_mock):
51 # Random.seed() uses time.time() when an operating system specific
52 # randomness source is not found. To test this on machines were it
53 # exists, run the above test, test_seedargs(), again after mocking
54 # os.urandom() so that it raises the exception expected when the
55 # randomness source is not available.
56 urandom_mock.side_effect = NotImplementedError
57 self.test_seedargs()
58
Antoine Pitrou5e394332012-11-04 02:10:33 +010059 def test_shuffle(self):
60 shuffle = self.gen.shuffle
61 lst = []
62 shuffle(lst)
63 self.assertEqual(lst, [])
64 lst = [37]
65 shuffle(lst)
66 self.assertEqual(lst, [37])
67 seqs = [list(range(n)) for n in range(10)]
68 shuffled_seqs = [list(range(n)) for n in range(10)]
69 for shuffled_seq in shuffled_seqs:
70 shuffle(shuffled_seq)
71 for (seq, shuffled_seq) in zip(seqs, shuffled_seqs):
72 self.assertEqual(len(seq), len(shuffled_seq))
73 self.assertEqual(set(seq), set(shuffled_seq))
Antoine Pitrou5e394332012-11-04 02:10:33 +010074 # The above tests all would pass if the shuffle was a
75 # no-op. The following non-deterministic test covers that. It
76 # asserts that the shuffled sequence of 1000 distinct elements
77 # must be different from the original one. Although there is
78 # mathematically a non-zero probability that this could
79 # actually happen in a genuinely random shuffle, it is
80 # completely negligible, given that the number of possible
81 # permutations of 1000 objects is 1000! (factorial of 1000),
82 # which is considerably larger than the number of atoms in the
83 # universe...
84 lst = list(range(1000))
85 shuffled_lst = list(range(1000))
86 shuffle(shuffled_lst)
87 self.assertTrue(lst != shuffled_lst)
88 shuffle(lst)
89 self.assertTrue(lst != shuffled_lst)
90
Raymond Hettingerdc4872e2010-09-07 10:06:56 +000091 def test_choice(self):
92 choice = self.gen.choice
93 with self.assertRaises(IndexError):
94 choice([])
95 self.assertEqual(choice([50]), 50)
96 self.assertIn(choice([25, 75]), [25, 75])
97
Raymond Hettinger40f62172002-12-29 23:03:38 +000098 def test_sample(self):
99 # For the entire allowable range of 0 <= k <= N, validate that
100 # the sample is of the correct length and contains only unique items
101 N = 100
Guido van Rossum805365e2007-05-07 22:24:25 +0000102 population = range(N)
103 for k in range(N+1):
Raymond Hettinger40f62172002-12-29 23:03:38 +0000104 s = self.gen.sample(population, k)
105 self.assertEqual(len(s), k)
Raymond Hettingera690a992003-11-16 16:17:49 +0000106 uniq = set(s)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000107 self.assertEqual(len(uniq), k)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000108 self.assertTrue(uniq <= set(population))
Raymond Hettinger8ec78812003-01-04 05:55:11 +0000109 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
R David Murraye3e1c172013-04-02 12:47:23 -0400110 # Exception raised if size of sample exceeds that of population
111 self.assertRaises(ValueError, self.gen.sample, population, N+1)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000112
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000113 def test_sample_distribution(self):
114 # For the entire allowable range of 0 <= k <= N, validate that
115 # sample generates all possible permutations
116 n = 5
117 pop = range(n)
118 trials = 10000 # large num prevents false negatives without slowing normal case
119 def factorial(n):
Guido van Rossum89da5d72006-08-22 00:21:25 +0000120 if n == 0:
121 return 1
122 return n * factorial(n - 1)
Guido van Rossum805365e2007-05-07 22:24:25 +0000123 for k in range(n):
Raymond Hettingerffdb8bb2004-09-27 15:29:05 +0000124 expected = factorial(n) // factorial(n-k)
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000125 perms = {}
Guido van Rossum805365e2007-05-07 22:24:25 +0000126 for i in range(trials):
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000127 perms[tuple(self.gen.sample(pop, k))] = None
128 if len(perms) == expected:
129 break
130 else:
131 self.fail()
132
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000133 def test_sample_inputs(self):
134 # SF bug #801342 -- population can be any iterable defining __len__()
Raymond Hettingera690a992003-11-16 16:17:49 +0000135 self.gen.sample(set(range(20)), 2)
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000136 self.gen.sample(range(20), 2)
Guido van Rossum805365e2007-05-07 22:24:25 +0000137 self.gen.sample(range(20), 2)
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000138 self.gen.sample(str('abcdefghijklmnopqrst'), 2)
139 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
140
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141 def test_sample_on_dicts(self):
Raymond Hettinger1acde192008-01-14 01:00:53 +0000142 self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143
Raymond Hettinger40f62172002-12-29 23:03:38 +0000144 def test_gauss(self):
145 # Ensure that the seed() method initializes all the hidden state. In
146 # particular, through 2.2.1 it failed to reset a piece of state used
147 # by (and only by) the .gauss() method.
148
149 for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
150 self.gen.seed(seed)
151 x1 = self.gen.random()
152 y1 = self.gen.gauss(0, 1)
153
154 self.gen.seed(seed)
155 x2 = self.gen.random()
156 y2 = self.gen.gauss(0, 1)
157
158 self.assertEqual(x1, x2)
159 self.assertEqual(y1, y2)
160
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000161 def test_pickling(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +0200162 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
163 state = pickle.dumps(self.gen, proto)
164 origseq = [self.gen.random() for i in range(10)]
165 newgen = pickle.loads(state)
166 restoredseq = [newgen.random() for i in range(10)]
167 self.assertEqual(origseq, restoredseq)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000168
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000169 def test_bug_1727780(self):
170 # verify that version-2-pickles can be loaded
171 # fine, whether they are created on 32-bit or 64-bit
172 # platforms, and that version-3-pickles load fine.
173 files = [("randv2_32.pck", 780),
174 ("randv2_64.pck", 866),
175 ("randv3.pck", 343)]
176 for file, value in files:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000177 f = open(support.findfile(file),"rb")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000178 r = pickle.load(f)
179 f.close()
Raymond Hettinger05156612010-09-07 04:44:52 +0000180 self.assertEqual(int(r.random()*1000), value)
181
182 def test_bug_9025(self):
183 # Had problem with an uneven distribution in int(n*random())
184 # Verify the fix by checking that distributions fall within expectations.
185 n = 100000
186 randrange = self.gen.randrange
187 k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
188 self.assertTrue(0.30 < k/n < .37, (k/n))
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000189
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300190try:
191 random.SystemRandom().random()
192except NotImplementedError:
193 SystemRandom_available = False
194else:
195 SystemRandom_available = True
196
197@unittest.skipUnless(SystemRandom_available, "random.SystemRandom not available")
198class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
Raymond Hettinger23f12412004-09-13 22:23:21 +0000199 gen = random.SystemRandom()
Raymond Hettinger356a4592004-08-30 06:14:31 +0000200
201 def test_autoseed(self):
202 # Doesn't need to do anything except not fail
203 self.gen.seed()
204
205 def test_saverestore(self):
206 self.assertRaises(NotImplementedError, self.gen.getstate)
207 self.assertRaises(NotImplementedError, self.gen.setstate, None)
208
209 def test_seedargs(self):
210 # Doesn't need to do anything except not fail
211 self.gen.seed(100)
212
Raymond Hettinger356a4592004-08-30 06:14:31 +0000213 def test_gauss(self):
214 self.gen.gauss_next = None
215 self.gen.seed(100)
216 self.assertEqual(self.gen.gauss_next, None)
217
218 def test_pickling(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +0200219 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
220 self.assertRaises(NotImplementedError, pickle.dumps, self.gen, proto)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000221
222 def test_53_bits_per_float(self):
223 # This should pass whenever a C double has 53 bit precision.
224 span = 2 ** 53
225 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000226 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000227 cum |= int(self.gen.random() * span)
228 self.assertEqual(cum, span-1)
229
230 def test_bigrand(self):
231 # The randrange routine should build-up the required number of bits
232 # in stages so that all bit positions are active.
233 span = 2 ** 500
234 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000235 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000236 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000237 self.assertTrue(0 <= r < span)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000238 cum |= r
239 self.assertEqual(cum, span-1)
240
241 def test_bigrand_ranges(self):
242 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600243 start = self.gen.randrange(2 ** (i-2))
244 stop = self.gen.randrange(2 ** i)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000245 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600246 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000247 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000248
249 def test_rangelimits(self):
250 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
251 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000252 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000253
R David Murraye3e1c172013-04-02 12:47:23 -0400254 def test_randrange_nonunit_step(self):
255 rint = self.gen.randrange(0, 10, 2)
256 self.assertIn(rint, (0, 2, 4, 6, 8))
257 rint = self.gen.randrange(0, 2, 2)
258 self.assertEqual(rint, 0)
259
260 def test_randrange_errors(self):
261 raises = partial(self.assertRaises, ValueError, self.gen.randrange)
262 # Empty range
263 raises(3, 3)
264 raises(-721)
265 raises(0, 100, -12)
266 # Non-integer start/stop
267 raises(3.14159)
268 raises(0, 2.71828)
269 # Zero and non-integer step
270 raises(0, 42, 0)
271 raises(0, 42, 3.14159)
272
Raymond Hettinger356a4592004-08-30 06:14:31 +0000273 def test_genrandbits(self):
274 # Verify ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000275 for k in range(1, 1000):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000276 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000277
278 # Verify all bits active
279 getbits = self.gen.getrandbits
280 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
281 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000282 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000283 cum |= getbits(span)
284 self.assertEqual(cum, 2**span-1)
285
286 # Verify argument checking
287 self.assertRaises(TypeError, self.gen.getrandbits)
288 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
289 self.assertRaises(ValueError, self.gen.getrandbits, 0)
290 self.assertRaises(ValueError, self.gen.getrandbits, -1)
291 self.assertRaises(TypeError, self.gen.getrandbits, 10.1)
292
293 def test_randbelow_logic(self, _log=log, int=int):
294 # check bitcount transition points: 2**i and 2**(i+1)-1
295 # show that: k = int(1.001 + _log(n, 2))
296 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000297 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000298 n = 1 << i # check an exact power of two
Raymond Hettinger356a4592004-08-30 06:14:31 +0000299 numbits = i+1
300 k = int(1.00001 + _log(n, 2))
301 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000302 self.assertEqual(n, 2**(k-1))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000303
304 n += n - 1 # check 1 below the next power of two
305 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000306 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000307 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000308
309 n -= n >> 15 # check a little farther below the next power of two
310 k = int(1.00001 + _log(n, 2))
311 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000312 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger356a4592004-08-30 06:14:31 +0000313
314
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300315class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
Raymond Hettinger40f62172002-12-29 23:03:38 +0000316 gen = random.Random()
317
Raymond Hettingerf763a722010-09-07 00:38:15 +0000318 def test_guaranteed_stable(self):
319 # These sequences are guaranteed to stay the same across versions of python
320 self.gen.seed(3456147, version=1)
321 self.assertEqual([self.gen.random().hex() for i in range(4)],
322 ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1',
323 '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000324 self.gen.seed("the quick brown fox", version=2)
325 self.assertEqual([self.gen.random().hex() for i in range(4)],
Raymond Hettinger3fcf0022010-12-08 01:13:53 +0000326 ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4',
327 '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000328
Raymond Hettingerc7bab7c2016-08-31 15:01:08 -0700329 def test_bug_27706(self):
330 # Verify that version 1 seeds are unaffected by hash randomization
331
332 self.gen.seed('nofar', version=1) # hash('nofar') == 5990528763808513177
333 self.assertEqual([self.gen.random().hex() for i in range(4)],
334 ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5',
335 '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6'])
336
337 self.gen.seed('rachel', version=1) # hash('rachel') == -9091735575445484789
338 self.assertEqual([self.gen.random().hex() for i in range(4)],
339 ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3',
340 '0x1.3052b9c072678p-2', '0x1.578f332106574p-3'])
341
342 self.gen.seed('', version=1) # hash('') == 0
343 self.assertEqual([self.gen.random().hex() for i in range(4)],
344 ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1',
345 '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2'])
346
Raymond Hettinger58335872004-07-09 14:26:18 +0000347 def test_setstate_first_arg(self):
348 self.assertRaises(ValueError, self.gen.setstate, (1, None, None))
349
350 def test_setstate_middle_arg(self):
351 # Wrong type, s/b tuple
352 self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
353 # Wrong length, s/b 625
354 self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None))
355 # Wrong type, s/b tuple of 625 ints
356 self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
357 # Last element s/b an int also
358 self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))
Serhiy Storchaka178f0b62015-07-24 09:02:53 +0300359 # Last element s/b between 0 and 624
360 with self.assertRaises((ValueError, OverflowError)):
361 self.gen.setstate((2, (1,)*624+(625,), None))
362 with self.assertRaises((ValueError, OverflowError)):
363 self.gen.setstate((2, (1,)*624+(-1,), None))
Raymond Hettinger58335872004-07-09 14:26:18 +0000364
R David Murraye3e1c172013-04-02 12:47:23 -0400365 # Little trick to make "tuple(x % (2**32) for x in internalstate)"
366 # raise ValueError. I cannot think of a simple way to achieve this, so
367 # I am opting for using a generator as the middle argument of setstate
368 # which attempts to cast a NaN to integer.
369 state_values = self.gen.getstate()[1]
370 state_values = list(state_values)
371 state_values[-1] = float('nan')
372 state = (int(x) for x in state_values)
373 self.assertRaises(TypeError, self.gen.setstate, (2, state, None))
374
Raymond Hettinger40f62172002-12-29 23:03:38 +0000375 def test_referenceImplementation(self):
376 # Compare the python implementation with results from the original
377 # code. Create 2000 53-bit precision random floats. Compare only
378 # the last ten entries to show that the independent implementations
379 # are tracking. Here is the main() function needed to create the
380 # list of expected random numbers:
381 # void main(void){
382 # int i;
383 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
384 # init_by_array(init, length);
385 # for (i=0; i<2000; i++) {
386 # printf("%.15f ", genrand_res53());
387 # if (i%5==4) printf("\n");
388 # }
389 # }
390 expected = [0.45839803073713259,
391 0.86057815201978782,
392 0.92848331726782152,
393 0.35932681119782461,
394 0.081823493762449573,
395 0.14332226470169329,
396 0.084297823823520024,
397 0.53814864671831453,
398 0.089215024911993401,
399 0.78486196105372907]
400
Guido van Rossume2a383d2007-01-15 16:59:06 +0000401 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000402 actual = self.randomlist(2000)[-10:]
403 for a, e in zip(actual, expected):
404 self.assertAlmostEqual(a,e,places=14)
405
406 def test_strong_reference_implementation(self):
407 # Like test_referenceImplementation, but checks for exact bit-level
408 # equality. This should pass on any box where C double contains
409 # at least 53 bits of precision (the underlying algorithm suffers
410 # no rounding errors -- all results are exact).
411 from math import ldexp
412
Guido van Rossume2a383d2007-01-15 16:59:06 +0000413 expected = [0x0eab3258d2231f,
414 0x1b89db315277a5,
415 0x1db622a5518016,
416 0x0b7f9af0d575bf,
417 0x029e4c4db82240,
418 0x04961892f5d673,
419 0x02b291598e4589,
420 0x11388382c15694,
421 0x02dad977c9e1fe,
422 0x191d96d4d334c6]
423 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000424 actual = self.randomlist(2000)[-10:]
425 for a, e in zip(actual, expected):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000426 self.assertEqual(int(ldexp(a, 53)), e)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000427
428 def test_long_seed(self):
429 # This is most interesting to run in debug mode, just to make sure
430 # nothing blows up. Under the covers, a dynamically resized array
431 # is allocated, consuming space proportional to the number of bits
432 # in the seed. Unfortunately, that's a quadratic-time algorithm,
433 # so don't make this horribly big.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000434 seed = (1 << (10000 * 8)) - 1 # about 10K bytes
Raymond Hettinger40f62172002-12-29 23:03:38 +0000435 self.gen.seed(seed)
436
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000437 def test_53_bits_per_float(self):
438 # This should pass whenever a C double has 53 bit precision.
439 span = 2 ** 53
440 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000441 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000442 cum |= int(self.gen.random() * span)
443 self.assertEqual(cum, span-1)
444
445 def test_bigrand(self):
446 # The randrange routine should build-up the required number of bits
447 # in stages so that all bit positions are active.
448 span = 2 ** 500
449 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000450 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000451 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000452 self.assertTrue(0 <= r < span)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000453 cum |= r
454 self.assertEqual(cum, span-1)
455
456 def test_bigrand_ranges(self):
457 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600458 start = self.gen.randrange(2 ** (i-2))
459 stop = self.gen.randrange(2 ** i)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000460 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600461 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000462 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000463
464 def test_rangelimits(self):
465 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
Raymond Hettingera690a992003-11-16 16:17:49 +0000466 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000467 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000468
469 def test_genrandbits(self):
470 # Verify cross-platform repeatability
471 self.gen.seed(1234567)
472 self.assertEqual(self.gen.getrandbits(100),
Guido van Rossume2a383d2007-01-15 16:59:06 +0000473 97904845777343510404718956115)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000474 # Verify ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000475 for k in range(1, 1000):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000476 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000477
478 # Verify all bits active
479 getbits = self.gen.getrandbits
480 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
481 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000482 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000483 cum |= getbits(span)
484 self.assertEqual(cum, 2**span-1)
485
Raymond Hettinger58335872004-07-09 14:26:18 +0000486 # Verify argument checking
487 self.assertRaises(TypeError, self.gen.getrandbits)
488 self.assertRaises(TypeError, self.gen.getrandbits, 'a')
489 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
490 self.assertRaises(ValueError, self.gen.getrandbits, 0)
491 self.assertRaises(ValueError, self.gen.getrandbits, -1)
492
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000493 def test_randbelow_logic(self, _log=log, int=int):
494 # check bitcount transition points: 2**i and 2**(i+1)-1
495 # show that: k = int(1.001 + _log(n, 2))
496 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000497 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000498 n = 1 << i # check an exact power of two
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000499 numbits = i+1
500 k = int(1.00001 + _log(n, 2))
501 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000502 self.assertEqual(n, 2**(k-1))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000503
504 n += n - 1 # check 1 below the next power of two
505 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000506 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000507 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000508
509 n -= n >> 15 # check a little farther below the next power of two
510 k = int(1.00001 + _log(n, 2))
511 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000512 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000513
R David Murraye3e1c172013-04-02 12:47:23 -0400514 @unittest.mock.patch('random.Random.random')
Martin Pantere26da7c2016-06-02 10:07:09 +0000515 def test_randbelow_overridden_random(self, random_mock):
R David Murraye3e1c172013-04-02 12:47:23 -0400516 # Random._randbelow() can only use random() when the built-in one
517 # has been overridden but no new getrandbits() method was supplied.
518 random_mock.side_effect = random.SystemRandom().random
519 maxsize = 1<<random.BPF
520 with warnings.catch_warnings():
521 warnings.simplefilter("ignore", UserWarning)
522 # Population range too large (n >= maxsize)
523 self.gen._randbelow(maxsize+1, maxsize = maxsize)
524 self.gen._randbelow(5640, maxsize = maxsize)
525
526 # This might be going too far to test a single line, but because of our
527 # noble aim of achieving 100% test coverage we need to write a case in
528 # which the following line in Random._randbelow() gets executed:
529 #
530 # rem = maxsize % n
531 # limit = (maxsize - rem) / maxsize
532 # r = random()
533 # while r >= limit:
534 # r = random() # <== *This line* <==<
535 #
536 # Therefore, to guarantee that the while loop is executed at least
537 # once, we need to mock random() so that it returns a number greater
538 # than 'limit' the first time it gets called.
539
540 n = 42
541 epsilon = 0.01
542 limit = (maxsize - (maxsize % n)) / maxsize
543 random_mock.side_effect = [limit + epsilon, limit - epsilon]
544 self.gen._randbelow(n, maxsize = maxsize)
545
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000546 def test_randrange_bug_1590891(self):
547 start = 1000000000000
548 stop = -100000000000000000000
549 step = -200
550 x = self.gen.randrange(start, stop, step)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000551 self.assertTrue(stop < x <= start)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000552 self.assertEqual((x+stop)%step, 0)
553
Raymond Hettinger2d0c2562009-02-19 09:53:18 +0000554def gamma(z, sqrt2pi=(2.0*pi)**0.5):
555 # Reflection to right half of complex plane
556 if z < 0.5:
557 return pi / sin(pi*z) / gamma(1.0-z)
558 # Lanczos approximation with g=7
559 az = z + (7.0 - 0.5)
560 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([
561 0.9999999999995183,
562 676.5203681218835 / z,
563 -1259.139216722289 / (z+1.0),
564 771.3234287757674 / (z+2.0),
565 -176.6150291498386 / (z+3.0),
566 12.50734324009056 / (z+4.0),
567 -0.1385710331296526 / (z+5.0),
568 0.9934937113930748e-05 / (z+6.0),
569 0.1659470187408462e-06 / (z+7.0),
570 ])
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000571
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000572class TestDistributions(unittest.TestCase):
573 def test_zeroinputs(self):
574 # Verify that distributions can handle a series of zero inputs'
575 g = random.Random()
Guido van Rossum805365e2007-05-07 22:24:25 +0000576 x = [g.random() for i in range(50)] + [0.0]*5
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000577 g.random = x[:].pop; g.uniform(1,10)
578 g.random = x[:].pop; g.paretovariate(1.0)
579 g.random = x[:].pop; g.expovariate(1.0)
580 g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200581 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000582 g.random = x[:].pop; g.normalvariate(0.0, 1.0)
583 g.random = x[:].pop; g.gauss(0.0, 1.0)
584 g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
585 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
586 g.random = x[:].pop; g.gammavariate(0.01, 1.0)
587 g.random = x[:].pop; g.gammavariate(1.0, 1.0)
588 g.random = x[:].pop; g.gammavariate(200.0, 1.0)
589 g.random = x[:].pop; g.betavariate(3.0, 3.0)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000590 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000591
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000592 def test_avg_std(self):
593 # Use integration to test distribution average and standard deviation.
594 # Only works for distributions which do not consume variates in pairs
595 g = random.Random()
596 N = 5000
Guido van Rossum805365e2007-05-07 22:24:25 +0000597 x = [i/float(N) for i in range(1,N)]
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000598 for variate, args, mu, sigmasqrd in [
599 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
Christian Heimesfe337bf2008-03-23 21:54:12 +0000600 (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0),
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000601 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200602 (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000603 (g.paretovariate, (5.0,), 5.0/(5.0-1),
604 5.0/((5.0-1)**2*(5.0-2))),
605 (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
606 gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]:
607 g.random = x[:].pop
608 y = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000609 for i in range(len(x)):
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000610 try:
611 y.append(variate(*args))
612 except IndexError:
613 pass
614 s1 = s2 = 0
615 for e in y:
616 s1 += e
617 s2 += (e - mu) ** 2
618 N = len(y)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200619 self.assertAlmostEqual(s1/N, mu, places=2,
620 msg='%s%r' % (variate.__name__, args))
621 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
622 msg='%s%r' % (variate.__name__, args))
623
624 def test_constant(self):
625 g = random.Random()
626 N = 100
627 for variate, args, expected in [
628 (g.uniform, (10.0, 10.0), 10.0),
629 (g.triangular, (10.0, 10.0), 10.0),
Raymond Hettinger978c6ab2014-05-25 17:25:27 -0700630 (g.triangular, (10.0, 10.0, 10.0), 10.0),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200631 (g.expovariate, (float('inf'),), 0.0),
632 (g.vonmisesvariate, (3.0, float('inf')), 3.0),
633 (g.gauss, (10.0, 0.0), 10.0),
634 (g.lognormvariate, (0.0, 0.0), 1.0),
635 (g.lognormvariate, (-float('inf'), 0.0), 0.0),
636 (g.normalvariate, (10.0, 0.0), 10.0),
637 (g.paretovariate, (float('inf'),), 1.0),
638 (g.weibullvariate, (10.0, float('inf')), 10.0),
639 (g.weibullvariate, (0.0, 10.0), 0.0),
640 ]:
641 for i in range(N):
642 self.assertEqual(variate(*args), expected)
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000643
Mark Dickinsonbe5f9192013-02-10 14:16:10 +0000644 def test_von_mises_range(self):
645 # Issue 17149: von mises variates were not consistently in the
646 # range [0, 2*PI].
647 g = random.Random()
648 N = 100
649 for mu in 0.0, 0.1, 3.1, 6.2:
650 for kappa in 0.0, 2.3, 500.0:
651 for _ in range(N):
652 sample = g.vonmisesvariate(mu, kappa)
653 self.assertTrue(
654 0 <= sample <= random.TWOPI,
655 msg=("vonmisesvariate({}, {}) produced a result {} out"
656 " of range [0, 2*pi]").format(mu, kappa, sample))
657
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200658 def test_von_mises_large_kappa(self):
659 # Issue #17141: vonmisesvariate() was hang for large kappas
660 random.vonmisesvariate(0, 1e15)
661 random.vonmisesvariate(0, 1e100)
662
R David Murraye3e1c172013-04-02 12:47:23 -0400663 def test_gammavariate_errors(self):
664 # Both alpha and beta must be > 0.0
665 self.assertRaises(ValueError, random.gammavariate, -1, 3)
666 self.assertRaises(ValueError, random.gammavariate, 0, 2)
667 self.assertRaises(ValueError, random.gammavariate, 2, 0)
668 self.assertRaises(ValueError, random.gammavariate, 1, -3)
669
670 @unittest.mock.patch('random.Random.random')
671 def test_gammavariate_full_code_coverage(self, random_mock):
672 # There are three different possibilities in the current implementation
673 # of random.gammavariate(), depending on the value of 'alpha'. What we
674 # are going to do here is to fix the values returned by random() to
675 # generate test cases that provide 100% line coverage of the method.
676
677 # #1: alpha > 1.0: we want the first random number to be outside the
678 # [1e-7, .9999999] range, so that the continue statement executes
679 # once. The values of u1 and u2 will be 0.5 and 0.3, respectively.
680 random_mock.side_effect = [1e-8, 0.5, 0.3]
681 returned_value = random.gammavariate(1.1, 2.3)
682 self.assertAlmostEqual(returned_value, 2.53)
683
684 # #2: alpha == 1: first random number less than 1e-7 to that the body
685 # of the while loop executes once. Then random.random() returns 0.45,
686 # which causes while to stop looping and the algorithm to terminate.
687 random_mock.side_effect = [1e-8, 0.45]
688 returned_value = random.gammavariate(1.0, 3.14)
689 self.assertAlmostEqual(returned_value, 2.507314166123803)
690
691 # #3: 0 < alpha < 1. This is the most complex region of code to cover,
692 # as there are multiple if-else statements. Let's take a look at the
693 # source code, and determine the values that we need accordingly:
694 #
695 # while 1:
696 # u = random()
697 # b = (_e + alpha)/_e
698 # p = b*u
699 # if p <= 1.0: # <=== (A)
700 # x = p ** (1.0/alpha)
701 # else: # <=== (B)
702 # x = -_log((b-p)/alpha)
703 # u1 = random()
704 # if p > 1.0: # <=== (C)
705 # if u1 <= x ** (alpha - 1.0): # <=== (D)
706 # break
707 # elif u1 <= _exp(-x): # <=== (E)
708 # break
709 # return x * beta
710 #
711 # First, we want (A) to be True. For that we need that:
712 # b*random() <= 1.0
713 # r1 = random() <= 1.0 / b
714 #
715 # We now get to the second if-else branch, and here, since p <= 1.0,
716 # (C) is False and we take the elif branch, (E). For it to be True,
717 # so that the break is executed, we need that:
718 # r2 = random() <= _exp(-x)
719 # r2 <= _exp(-(p ** (1.0/alpha)))
720 # r2 <= _exp(-((b*r1) ** (1.0/alpha)))
721
722 _e = random._e
723 _exp = random._exp
724 _log = random._log
725 alpha = 0.35
726 beta = 1.45
727 b = (_e + alpha)/_e
728 epsilon = 0.01
729
730 r1 = 0.8859296441566 # 1.0 / b
731 r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha)))
732
733 # These four "random" values result in the following trace:
734 # (A) True, (E) False --> [next iteration of while]
735 # (A) True, (E) True --> [while loop breaks]
736 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
737 returned_value = random.gammavariate(alpha, beta)
738 self.assertAlmostEqual(returned_value, 1.4499999999997544)
739
740 # Let's now make (A) be False. If this is the case, when we get to the
741 # second if-else 'p' is greater than 1, so (C) evaluates to True. We
742 # now encounter a second if statement, (D), which in order to execute
743 # must satisfy the following condition:
744 # r2 <= x ** (alpha - 1.0)
745 # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0)
746 # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0)
747 r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False
748 r2 = 0.9445400408898141
749
750 # And these four values result in the following trace:
751 # (B) and (C) True, (D) False --> [next iteration of while]
752 # (B) and (C) True, (D) True [while loop breaks]
753 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
754 returned_value = random.gammavariate(alpha, beta)
755 self.assertAlmostEqual(returned_value, 1.5830349561760781)
756
757 @unittest.mock.patch('random.Random.gammavariate')
758 def test_betavariate_return_zero(self, gammavariate_mock):
759 # betavariate() returns zero when the Gamma distribution
760 # that it uses internally returns this same value.
761 gammavariate_mock.return_value = 0.0
762 self.assertEqual(0.0, random.betavariate(2.71828, 3.14159))
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200763
Raymond Hettinger40f62172002-12-29 23:03:38 +0000764class TestModule(unittest.TestCase):
765 def testMagicConstants(self):
766 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
767 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
768 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
769 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
770
771 def test__all__(self):
772 # tests validity but not completeness of the __all__ list
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000773 self.assertTrue(set(random.__all__) <= set(dir(random)))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000774
Thomas Woutersb2137042007-02-01 18:02:27 +0000775 def test_random_subclass_with_kwargs(self):
776 # SF bug #1486663 -- this used to erroneously raise a TypeError
777 class Subclass(random.Random):
778 def __init__(self, newarg=None):
779 random.Random.__init__(self)
780 Subclass(newarg=1)
781
782
Raymond Hettinger40f62172002-12-29 23:03:38 +0000783if __name__ == "__main__":
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300784 unittest.main()