blob: e64804556db85bf118d34666d5cd2ffb9a4d4128 [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 Hettinger58335872004-07-09 14:26:18 +0000329 def test_setstate_first_arg(self):
330 self.assertRaises(ValueError, self.gen.setstate, (1, None, None))
331
332 def test_setstate_middle_arg(self):
333 # Wrong type, s/b tuple
334 self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
335 # Wrong length, s/b 625
336 self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None))
337 # Wrong type, s/b tuple of 625 ints
338 self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
339 # Last element s/b an int also
340 self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))
341
R David Murraye3e1c172013-04-02 12:47:23 -0400342 # Little trick to make "tuple(x % (2**32) for x in internalstate)"
343 # raise ValueError. I cannot think of a simple way to achieve this, so
344 # I am opting for using a generator as the middle argument of setstate
345 # which attempts to cast a NaN to integer.
346 state_values = self.gen.getstate()[1]
347 state_values = list(state_values)
348 state_values[-1] = float('nan')
349 state = (int(x) for x in state_values)
350 self.assertRaises(TypeError, self.gen.setstate, (2, state, None))
351
Raymond Hettinger40f62172002-12-29 23:03:38 +0000352 def test_referenceImplementation(self):
353 # Compare the python implementation with results from the original
354 # code. Create 2000 53-bit precision random floats. Compare only
355 # the last ten entries to show that the independent implementations
356 # are tracking. Here is the main() function needed to create the
357 # list of expected random numbers:
358 # void main(void){
359 # int i;
360 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
361 # init_by_array(init, length);
362 # for (i=0; i<2000; i++) {
363 # printf("%.15f ", genrand_res53());
364 # if (i%5==4) printf("\n");
365 # }
366 # }
367 expected = [0.45839803073713259,
368 0.86057815201978782,
369 0.92848331726782152,
370 0.35932681119782461,
371 0.081823493762449573,
372 0.14332226470169329,
373 0.084297823823520024,
374 0.53814864671831453,
375 0.089215024911993401,
376 0.78486196105372907]
377
Guido van Rossume2a383d2007-01-15 16:59:06 +0000378 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000379 actual = self.randomlist(2000)[-10:]
380 for a, e in zip(actual, expected):
381 self.assertAlmostEqual(a,e,places=14)
382
383 def test_strong_reference_implementation(self):
384 # Like test_referenceImplementation, but checks for exact bit-level
385 # equality. This should pass on any box where C double contains
386 # at least 53 bits of precision (the underlying algorithm suffers
387 # no rounding errors -- all results are exact).
388 from math import ldexp
389
Guido van Rossume2a383d2007-01-15 16:59:06 +0000390 expected = [0x0eab3258d2231f,
391 0x1b89db315277a5,
392 0x1db622a5518016,
393 0x0b7f9af0d575bf,
394 0x029e4c4db82240,
395 0x04961892f5d673,
396 0x02b291598e4589,
397 0x11388382c15694,
398 0x02dad977c9e1fe,
399 0x191d96d4d334c6]
400 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000401 actual = self.randomlist(2000)[-10:]
402 for a, e in zip(actual, expected):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000403 self.assertEqual(int(ldexp(a, 53)), e)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000404
405 def test_long_seed(self):
406 # This is most interesting to run in debug mode, just to make sure
407 # nothing blows up. Under the covers, a dynamically resized array
408 # is allocated, consuming space proportional to the number of bits
409 # in the seed. Unfortunately, that's a quadratic-time algorithm,
410 # so don't make this horribly big.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000411 seed = (1 << (10000 * 8)) - 1 # about 10K bytes
Raymond Hettinger40f62172002-12-29 23:03:38 +0000412 self.gen.seed(seed)
413
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000414 def test_53_bits_per_float(self):
415 # This should pass whenever a C double has 53 bit precision.
416 span = 2 ** 53
417 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000418 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000419 cum |= int(self.gen.random() * span)
420 self.assertEqual(cum, span-1)
421
422 def test_bigrand(self):
423 # The randrange routine should build-up the required number of bits
424 # in stages so that all bit positions are active.
425 span = 2 ** 500
426 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000427 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000428 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000429 self.assertTrue(0 <= r < span)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000430 cum |= r
431 self.assertEqual(cum, span-1)
432
433 def test_bigrand_ranges(self):
434 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600435 start = self.gen.randrange(2 ** (i-2))
436 stop = self.gen.randrange(2 ** i)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000437 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600438 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000439 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000440
441 def test_rangelimits(self):
442 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
Raymond Hettingera690a992003-11-16 16:17:49 +0000443 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000444 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000445
446 def test_genrandbits(self):
447 # Verify cross-platform repeatability
448 self.gen.seed(1234567)
449 self.assertEqual(self.gen.getrandbits(100),
Guido van Rossume2a383d2007-01-15 16:59:06 +0000450 97904845777343510404718956115)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000451 # Verify ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000452 for k in range(1, 1000):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000453 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000454
455 # Verify all bits active
456 getbits = self.gen.getrandbits
457 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
458 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000459 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000460 cum |= getbits(span)
461 self.assertEqual(cum, 2**span-1)
462
Raymond Hettinger58335872004-07-09 14:26:18 +0000463 # Verify argument checking
464 self.assertRaises(TypeError, self.gen.getrandbits)
465 self.assertRaises(TypeError, self.gen.getrandbits, 'a')
466 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
467 self.assertRaises(ValueError, self.gen.getrandbits, 0)
468 self.assertRaises(ValueError, self.gen.getrandbits, -1)
469
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000470 def test_randbelow_logic(self, _log=log, int=int):
471 # check bitcount transition points: 2**i and 2**(i+1)-1
472 # show that: k = int(1.001 + _log(n, 2))
473 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000474 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000475 n = 1 << i # check an exact power of two
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000476 numbits = i+1
477 k = int(1.00001 + _log(n, 2))
478 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000479 self.assertEqual(n, 2**(k-1))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000480
481 n += n - 1 # check 1 below the next power of two
482 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000483 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000484 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000485
486 n -= n >> 15 # check a little farther below the next power of two
487 k = int(1.00001 + _log(n, 2))
488 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000489 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000490
R David Murraye3e1c172013-04-02 12:47:23 -0400491 @unittest.mock.patch('random.Random.random')
492 def test_randbelow_overriden_random(self, random_mock):
493 # Random._randbelow() can only use random() when the built-in one
494 # has been overridden but no new getrandbits() method was supplied.
495 random_mock.side_effect = random.SystemRandom().random
496 maxsize = 1<<random.BPF
497 with warnings.catch_warnings():
498 warnings.simplefilter("ignore", UserWarning)
499 # Population range too large (n >= maxsize)
500 self.gen._randbelow(maxsize+1, maxsize = maxsize)
501 self.gen._randbelow(5640, maxsize = maxsize)
502
503 # This might be going too far to test a single line, but because of our
504 # noble aim of achieving 100% test coverage we need to write a case in
505 # which the following line in Random._randbelow() gets executed:
506 #
507 # rem = maxsize % n
508 # limit = (maxsize - rem) / maxsize
509 # r = random()
510 # while r >= limit:
511 # r = random() # <== *This line* <==<
512 #
513 # Therefore, to guarantee that the while loop is executed at least
514 # once, we need to mock random() so that it returns a number greater
515 # than 'limit' the first time it gets called.
516
517 n = 42
518 epsilon = 0.01
519 limit = (maxsize - (maxsize % n)) / maxsize
520 random_mock.side_effect = [limit + epsilon, limit - epsilon]
521 self.gen._randbelow(n, maxsize = maxsize)
522
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000523 def test_randrange_bug_1590891(self):
524 start = 1000000000000
525 stop = -100000000000000000000
526 step = -200
527 x = self.gen.randrange(start, stop, step)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000528 self.assertTrue(stop < x <= start)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000529 self.assertEqual((x+stop)%step, 0)
530
Raymond Hettinger2d0c2562009-02-19 09:53:18 +0000531def gamma(z, sqrt2pi=(2.0*pi)**0.5):
532 # Reflection to right half of complex plane
533 if z < 0.5:
534 return pi / sin(pi*z) / gamma(1.0-z)
535 # Lanczos approximation with g=7
536 az = z + (7.0 - 0.5)
537 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([
538 0.9999999999995183,
539 676.5203681218835 / z,
540 -1259.139216722289 / (z+1.0),
541 771.3234287757674 / (z+2.0),
542 -176.6150291498386 / (z+3.0),
543 12.50734324009056 / (z+4.0),
544 -0.1385710331296526 / (z+5.0),
545 0.9934937113930748e-05 / (z+6.0),
546 0.1659470187408462e-06 / (z+7.0),
547 ])
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000548
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000549class TestDistributions(unittest.TestCase):
550 def test_zeroinputs(self):
551 # Verify that distributions can handle a series of zero inputs'
552 g = random.Random()
Guido van Rossum805365e2007-05-07 22:24:25 +0000553 x = [g.random() for i in range(50)] + [0.0]*5
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000554 g.random = x[:].pop; g.uniform(1,10)
555 g.random = x[:].pop; g.paretovariate(1.0)
556 g.random = x[:].pop; g.expovariate(1.0)
557 g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200558 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000559 g.random = x[:].pop; g.normalvariate(0.0, 1.0)
560 g.random = x[:].pop; g.gauss(0.0, 1.0)
561 g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
562 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
563 g.random = x[:].pop; g.gammavariate(0.01, 1.0)
564 g.random = x[:].pop; g.gammavariate(1.0, 1.0)
565 g.random = x[:].pop; g.gammavariate(200.0, 1.0)
566 g.random = x[:].pop; g.betavariate(3.0, 3.0)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000567 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000568
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000569 def test_avg_std(self):
570 # Use integration to test distribution average and standard deviation.
571 # Only works for distributions which do not consume variates in pairs
572 g = random.Random()
573 N = 5000
Guido van Rossum805365e2007-05-07 22:24:25 +0000574 x = [i/float(N) for i in range(1,N)]
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000575 for variate, args, mu, sigmasqrd in [
576 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
Christian Heimesfe337bf2008-03-23 21:54:12 +0000577 (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 +0000578 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200579 (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000580 (g.paretovariate, (5.0,), 5.0/(5.0-1),
581 5.0/((5.0-1)**2*(5.0-2))),
582 (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
583 gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]:
584 g.random = x[:].pop
585 y = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000586 for i in range(len(x)):
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000587 try:
588 y.append(variate(*args))
589 except IndexError:
590 pass
591 s1 = s2 = 0
592 for e in y:
593 s1 += e
594 s2 += (e - mu) ** 2
595 N = len(y)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200596 self.assertAlmostEqual(s1/N, mu, places=2,
597 msg='%s%r' % (variate.__name__, args))
598 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
599 msg='%s%r' % (variate.__name__, args))
600
601 def test_constant(self):
602 g = random.Random()
603 N = 100
604 for variate, args, expected in [
605 (g.uniform, (10.0, 10.0), 10.0),
606 (g.triangular, (10.0, 10.0), 10.0),
Raymond Hettinger978c6ab2014-05-25 17:25:27 -0700607 (g.triangular, (10.0, 10.0, 10.0), 10.0),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200608 (g.expovariate, (float('inf'),), 0.0),
609 (g.vonmisesvariate, (3.0, float('inf')), 3.0),
610 (g.gauss, (10.0, 0.0), 10.0),
611 (g.lognormvariate, (0.0, 0.0), 1.0),
612 (g.lognormvariate, (-float('inf'), 0.0), 0.0),
613 (g.normalvariate, (10.0, 0.0), 10.0),
614 (g.paretovariate, (float('inf'),), 1.0),
615 (g.weibullvariate, (10.0, float('inf')), 10.0),
616 (g.weibullvariate, (0.0, 10.0), 0.0),
617 ]:
618 for i in range(N):
619 self.assertEqual(variate(*args), expected)
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000620
Mark Dickinsonbe5f9192013-02-10 14:16:10 +0000621 def test_von_mises_range(self):
622 # Issue 17149: von mises variates were not consistently in the
623 # range [0, 2*PI].
624 g = random.Random()
625 N = 100
626 for mu in 0.0, 0.1, 3.1, 6.2:
627 for kappa in 0.0, 2.3, 500.0:
628 for _ in range(N):
629 sample = g.vonmisesvariate(mu, kappa)
630 self.assertTrue(
631 0 <= sample <= random.TWOPI,
632 msg=("vonmisesvariate({}, {}) produced a result {} out"
633 " of range [0, 2*pi]").format(mu, kappa, sample))
634
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200635 def test_von_mises_large_kappa(self):
636 # Issue #17141: vonmisesvariate() was hang for large kappas
637 random.vonmisesvariate(0, 1e15)
638 random.vonmisesvariate(0, 1e100)
639
R David Murraye3e1c172013-04-02 12:47:23 -0400640 def test_gammavariate_errors(self):
641 # Both alpha and beta must be > 0.0
642 self.assertRaises(ValueError, random.gammavariate, -1, 3)
643 self.assertRaises(ValueError, random.gammavariate, 0, 2)
644 self.assertRaises(ValueError, random.gammavariate, 2, 0)
645 self.assertRaises(ValueError, random.gammavariate, 1, -3)
646
647 @unittest.mock.patch('random.Random.random')
648 def test_gammavariate_full_code_coverage(self, random_mock):
649 # There are three different possibilities in the current implementation
650 # of random.gammavariate(), depending on the value of 'alpha'. What we
651 # are going to do here is to fix the values returned by random() to
652 # generate test cases that provide 100% line coverage of the method.
653
654 # #1: alpha > 1.0: we want the first random number to be outside the
655 # [1e-7, .9999999] range, so that the continue statement executes
656 # once. The values of u1 and u2 will be 0.5 and 0.3, respectively.
657 random_mock.side_effect = [1e-8, 0.5, 0.3]
658 returned_value = random.gammavariate(1.1, 2.3)
659 self.assertAlmostEqual(returned_value, 2.53)
660
661 # #2: alpha == 1: first random number less than 1e-7 to that the body
662 # of the while loop executes once. Then random.random() returns 0.45,
663 # which causes while to stop looping and the algorithm to terminate.
664 random_mock.side_effect = [1e-8, 0.45]
665 returned_value = random.gammavariate(1.0, 3.14)
666 self.assertAlmostEqual(returned_value, 2.507314166123803)
667
668 # #3: 0 < alpha < 1. This is the most complex region of code to cover,
669 # as there are multiple if-else statements. Let's take a look at the
670 # source code, and determine the values that we need accordingly:
671 #
672 # while 1:
673 # u = random()
674 # b = (_e + alpha)/_e
675 # p = b*u
676 # if p <= 1.0: # <=== (A)
677 # x = p ** (1.0/alpha)
678 # else: # <=== (B)
679 # x = -_log((b-p)/alpha)
680 # u1 = random()
681 # if p > 1.0: # <=== (C)
682 # if u1 <= x ** (alpha - 1.0): # <=== (D)
683 # break
684 # elif u1 <= _exp(-x): # <=== (E)
685 # break
686 # return x * beta
687 #
688 # First, we want (A) to be True. For that we need that:
689 # b*random() <= 1.0
690 # r1 = random() <= 1.0 / b
691 #
692 # We now get to the second if-else branch, and here, since p <= 1.0,
693 # (C) is False and we take the elif branch, (E). For it to be True,
694 # so that the break is executed, we need that:
695 # r2 = random() <= _exp(-x)
696 # r2 <= _exp(-(p ** (1.0/alpha)))
697 # r2 <= _exp(-((b*r1) ** (1.0/alpha)))
698
699 _e = random._e
700 _exp = random._exp
701 _log = random._log
702 alpha = 0.35
703 beta = 1.45
704 b = (_e + alpha)/_e
705 epsilon = 0.01
706
707 r1 = 0.8859296441566 # 1.0 / b
708 r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha)))
709
710 # These four "random" values result in the following trace:
711 # (A) True, (E) False --> [next iteration of while]
712 # (A) True, (E) True --> [while loop breaks]
713 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
714 returned_value = random.gammavariate(alpha, beta)
715 self.assertAlmostEqual(returned_value, 1.4499999999997544)
716
717 # Let's now make (A) be False. If this is the case, when we get to the
718 # second if-else 'p' is greater than 1, so (C) evaluates to True. We
719 # now encounter a second if statement, (D), which in order to execute
720 # must satisfy the following condition:
721 # r2 <= x ** (alpha - 1.0)
722 # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0)
723 # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0)
724 r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False
725 r2 = 0.9445400408898141
726
727 # And these four values result in the following trace:
728 # (B) and (C) True, (D) False --> [next iteration of while]
729 # (B) and (C) True, (D) True [while loop breaks]
730 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
731 returned_value = random.gammavariate(alpha, beta)
732 self.assertAlmostEqual(returned_value, 1.5830349561760781)
733
734 @unittest.mock.patch('random.Random.gammavariate')
735 def test_betavariate_return_zero(self, gammavariate_mock):
736 # betavariate() returns zero when the Gamma distribution
737 # that it uses internally returns this same value.
738 gammavariate_mock.return_value = 0.0
739 self.assertEqual(0.0, random.betavariate(2.71828, 3.14159))
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200740
Raymond Hettinger40f62172002-12-29 23:03:38 +0000741class TestModule(unittest.TestCase):
742 def testMagicConstants(self):
743 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
744 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
745 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
746 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
747
748 def test__all__(self):
749 # tests validity but not completeness of the __all__ list
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000750 self.assertTrue(set(random.__all__) <= set(dir(random)))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000751
Thomas Woutersb2137042007-02-01 18:02:27 +0000752 def test_random_subclass_with_kwargs(self):
753 # SF bug #1486663 -- this used to erroneously raise a TypeError
754 class Subclass(random.Random):
755 def __init__(self, newarg=None):
756 random.Random.__init__(self)
757 Subclass(newarg=1)
758
759
Raymond Hettinger40f62172002-12-29 23:03:38 +0000760if __name__ == "__main__":
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300761 unittest.main()