blob: ed5a48a4bf99c4281b04272a3093907359c81261 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Raymond Hettinger40f62172002-12-29 23:03:38 +00002
3import unittest
R David Murraye3e1c172013-04-02 12:47:23 -04004import unittest.mock
Tim Peters46c04e12002-05-05 20:40:00 +00005import random
Raymond Hettinger40f62172002-12-29 23:03:38 +00006import time
Raymond Hettinger5f078ff2003-06-24 20:29:04 +00007import pickle
Raymond Hettinger2f726e92003-10-05 09:09:15 +00008import warnings
R David Murraye3e1c172013-04-02 12:47:23 -04009from functools import partial
Georg Brandl1b37e872010-03-14 10:45:50 +000010from math import log, exp, pi, fsum, sin
Benjamin Petersonee8712c2008-05-20 21:35:26 +000011from test import support
Tim Peters46c04e12002-05-05 20:40:00 +000012
Raymond Hettinger40f62172002-12-29 23:03:38 +000013class TestBasicOps(unittest.TestCase):
14 # Superclass with tests common to all generators.
15 # Subclasses must arrange for self.gen to retrieve the Random instance
16 # to be tested.
Tim Peters46c04e12002-05-05 20:40:00 +000017
Raymond Hettinger40f62172002-12-29 23:03:38 +000018 def randomlist(self, n):
19 """Helper function to make a list of random numbers"""
Guido van Rossum805365e2007-05-07 22:24:25 +000020 return [self.gen.random() for i in range(n)]
Tim Peters46c04e12002-05-05 20:40:00 +000021
Raymond Hettinger40f62172002-12-29 23:03:38 +000022 def test_autoseed(self):
23 self.gen.seed()
24 state1 = self.gen.getstate()
Raymond Hettinger3081d592003-08-09 18:30:57 +000025 time.sleep(0.1)
Raymond Hettinger40f62172002-12-29 23:03:38 +000026 self.gen.seed() # diffent seeds at different times
27 state2 = self.gen.getstate()
28 self.assertNotEqual(state1, state2)
Tim Peters46c04e12002-05-05 20:40:00 +000029
Raymond Hettinger40f62172002-12-29 23:03:38 +000030 def test_saverestore(self):
31 N = 1000
32 self.gen.seed()
33 state = self.gen.getstate()
34 randseq = self.randomlist(N)
35 self.gen.setstate(state) # should regenerate the same sequence
36 self.assertEqual(randseq, self.randomlist(N))
37
38 def test_seedargs(self):
Mark Dickinson95aeae02012-06-24 11:05:30 +010039 # Seed value with a negative hash.
40 class MySeed(object):
41 def __hash__(self):
42 return -1729
Guido van Rossume2a383d2007-01-15 16:59:06 +000043 for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20),
Mark Dickinson95aeae02012-06-24 11:05:30 +010044 3.14, 1+2j, 'a', tuple('abc'), MySeed()]:
Raymond Hettinger40f62172002-12-29 23:03:38 +000045 self.gen.seed(arg)
Guido van Rossum805365e2007-05-07 22:24:25 +000046 for arg in [list(range(3)), dict(one=1)]:
Raymond Hettinger40f62172002-12-29 23:03:38 +000047 self.assertRaises(TypeError, self.gen.seed, arg)
Raymond Hettingerf763a722010-09-07 00:38:15 +000048 self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
Raymond Hettinger58335872004-07-09 14:26:18 +000049 self.assertRaises(TypeError, type(self.gen), [])
Raymond Hettinger40f62172002-12-29 23:03:38 +000050
R David Murraye3e1c172013-04-02 12:47:23 -040051 @unittest.mock.patch('random._urandom') # os.urandom
52 def test_seed_when_randomness_source_not_found(self, urandom_mock):
53 # Random.seed() uses time.time() when an operating system specific
54 # randomness source is not found. To test this on machines were it
55 # exists, run the above test, test_seedargs(), again after mocking
56 # os.urandom() so that it raises the exception expected when the
57 # randomness source is not available.
58 urandom_mock.side_effect = NotImplementedError
59 self.test_seedargs()
60
Antoine Pitrou5e394332012-11-04 02:10:33 +010061 def test_shuffle(self):
62 shuffle = self.gen.shuffle
63 lst = []
64 shuffle(lst)
65 self.assertEqual(lst, [])
66 lst = [37]
67 shuffle(lst)
68 self.assertEqual(lst, [37])
69 seqs = [list(range(n)) for n in range(10)]
70 shuffled_seqs = [list(range(n)) for n in range(10)]
71 for shuffled_seq in shuffled_seqs:
72 shuffle(shuffled_seq)
73 for (seq, shuffled_seq) in zip(seqs, shuffled_seqs):
74 self.assertEqual(len(seq), len(shuffled_seq))
75 self.assertEqual(set(seq), set(shuffled_seq))
76
77 # The above tests all would pass if the shuffle was a
78 # no-op. The following non-deterministic test covers that. It
79 # asserts that the shuffled sequence of 1000 distinct elements
80 # must be different from the original one. Although there is
81 # mathematically a non-zero probability that this could
82 # actually happen in a genuinely random shuffle, it is
83 # completely negligible, given that the number of possible
84 # permutations of 1000 objects is 1000! (factorial of 1000),
85 # which is considerably larger than the number of atoms in the
86 # universe...
87 lst = list(range(1000))
88 shuffled_lst = list(range(1000))
89 shuffle(shuffled_lst)
90 self.assertTrue(lst != shuffled_lst)
91 shuffle(lst)
92 self.assertTrue(lst != shuffled_lst)
93
Raymond Hettingerdc4872e2010-09-07 10:06:56 +000094 def test_choice(self):
95 choice = self.gen.choice
96 with self.assertRaises(IndexError):
97 choice([])
98 self.assertEqual(choice([50]), 50)
99 self.assertIn(choice([25, 75]), [25, 75])
100
Raymond Hettinger40f62172002-12-29 23:03:38 +0000101 def test_sample(self):
102 # For the entire allowable range of 0 <= k <= N, validate that
103 # the sample is of the correct length and contains only unique items
104 N = 100
Guido van Rossum805365e2007-05-07 22:24:25 +0000105 population = range(N)
106 for k in range(N+1):
Raymond Hettinger40f62172002-12-29 23:03:38 +0000107 s = self.gen.sample(population, k)
108 self.assertEqual(len(s), k)
Raymond Hettingera690a992003-11-16 16:17:49 +0000109 uniq = set(s)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000110 self.assertEqual(len(uniq), k)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000111 self.assertTrue(uniq <= set(population))
Raymond Hettinger8ec78812003-01-04 05:55:11 +0000112 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
R David Murraye3e1c172013-04-02 12:47:23 -0400113 # Exception raised if size of sample exceeds that of population
114 self.assertRaises(ValueError, self.gen.sample, population, N+1)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000115
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000116 def test_sample_distribution(self):
117 # For the entire allowable range of 0 <= k <= N, validate that
118 # sample generates all possible permutations
119 n = 5
120 pop = range(n)
121 trials = 10000 # large num prevents false negatives without slowing normal case
122 def factorial(n):
Guido van Rossum89da5d72006-08-22 00:21:25 +0000123 if n == 0:
124 return 1
125 return n * factorial(n - 1)
Guido van Rossum805365e2007-05-07 22:24:25 +0000126 for k in range(n):
Raymond Hettingerffdb8bb2004-09-27 15:29:05 +0000127 expected = factorial(n) // factorial(n-k)
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000128 perms = {}
Guido van Rossum805365e2007-05-07 22:24:25 +0000129 for i in range(trials):
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000130 perms[tuple(self.gen.sample(pop, k))] = None
131 if len(perms) == expected:
132 break
133 else:
134 self.fail()
135
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000136 def test_sample_inputs(self):
137 # SF bug #801342 -- population can be any iterable defining __len__()
Raymond Hettingera690a992003-11-16 16:17:49 +0000138 self.gen.sample(set(range(20)), 2)
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000139 self.gen.sample(range(20), 2)
Guido van Rossum805365e2007-05-07 22:24:25 +0000140 self.gen.sample(range(20), 2)
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000141 self.gen.sample(str('abcdefghijklmnopqrst'), 2)
142 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
143
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 def test_sample_on_dicts(self):
Raymond Hettinger1acde192008-01-14 01:00:53 +0000145 self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146
Raymond Hettinger40f62172002-12-29 23:03:38 +0000147 def test_gauss(self):
148 # Ensure that the seed() method initializes all the hidden state. In
149 # particular, through 2.2.1 it failed to reset a piece of state used
150 # by (and only by) the .gauss() method.
151
152 for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
153 self.gen.seed(seed)
154 x1 = self.gen.random()
155 y1 = self.gen.gauss(0, 1)
156
157 self.gen.seed(seed)
158 x2 = self.gen.random()
159 y2 = self.gen.gauss(0, 1)
160
161 self.assertEqual(x1, x2)
162 self.assertEqual(y1, y2)
163
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000164 def test_pickling(self):
165 state = pickle.dumps(self.gen)
Guido van Rossum805365e2007-05-07 22:24:25 +0000166 origseq = [self.gen.random() for i in range(10)]
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000167 newgen = pickle.loads(state)
Guido van Rossum805365e2007-05-07 22:24:25 +0000168 restoredseq = [newgen.random() for i in range(10)]
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000169 self.assertEqual(origseq, restoredseq)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000170
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000171 def test_bug_1727780(self):
172 # verify that version-2-pickles can be loaded
173 # fine, whether they are created on 32-bit or 64-bit
174 # platforms, and that version-3-pickles load fine.
175 files = [("randv2_32.pck", 780),
176 ("randv2_64.pck", 866),
177 ("randv3.pck", 343)]
178 for file, value in files:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000179 f = open(support.findfile(file),"rb")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000180 r = pickle.load(f)
181 f.close()
Raymond Hettinger05156612010-09-07 04:44:52 +0000182 self.assertEqual(int(r.random()*1000), value)
183
184 def test_bug_9025(self):
185 # Had problem with an uneven distribution in int(n*random())
186 # Verify the fix by checking that distributions fall within expectations.
187 n = 100000
188 randrange = self.gen.randrange
189 k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
190 self.assertTrue(0.30 < k/n < .37, (k/n))
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000191
Raymond Hettinger23f12412004-09-13 22:23:21 +0000192class SystemRandom_TestBasicOps(TestBasicOps):
193 gen = random.SystemRandom()
Raymond Hettinger356a4592004-08-30 06:14:31 +0000194
195 def test_autoseed(self):
196 # Doesn't need to do anything except not fail
197 self.gen.seed()
198
199 def test_saverestore(self):
200 self.assertRaises(NotImplementedError, self.gen.getstate)
201 self.assertRaises(NotImplementedError, self.gen.setstate, None)
202
203 def test_seedargs(self):
204 # Doesn't need to do anything except not fail
205 self.gen.seed(100)
206
Raymond Hettinger356a4592004-08-30 06:14:31 +0000207 def test_gauss(self):
208 self.gen.gauss_next = None
209 self.gen.seed(100)
210 self.assertEqual(self.gen.gauss_next, None)
211
212 def test_pickling(self):
213 self.assertRaises(NotImplementedError, pickle.dumps, self.gen)
214
215 def test_53_bits_per_float(self):
216 # This should pass whenever a C double has 53 bit precision.
217 span = 2 ** 53
218 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000219 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000220 cum |= int(self.gen.random() * span)
221 self.assertEqual(cum, span-1)
222
223 def test_bigrand(self):
224 # The randrange routine should build-up the required number of bits
225 # in stages so that all bit positions are active.
226 span = 2 ** 500
227 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000228 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000229 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000230 self.assertTrue(0 <= r < span)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000231 cum |= r
232 self.assertEqual(cum, span-1)
233
234 def test_bigrand_ranges(self):
235 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
236 start = self.gen.randrange(2 ** i)
237 stop = self.gen.randrange(2 ** (i-2))
238 if stop <= start:
239 return
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000240 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000241
242 def test_rangelimits(self):
243 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
244 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000245 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000246
R David Murraye3e1c172013-04-02 12:47:23 -0400247 def test_randrange_nonunit_step(self):
248 rint = self.gen.randrange(0, 10, 2)
249 self.assertIn(rint, (0, 2, 4, 6, 8))
250 rint = self.gen.randrange(0, 2, 2)
251 self.assertEqual(rint, 0)
252
253 def test_randrange_errors(self):
254 raises = partial(self.assertRaises, ValueError, self.gen.randrange)
255 # Empty range
256 raises(3, 3)
257 raises(-721)
258 raises(0, 100, -12)
259 # Non-integer start/stop
260 raises(3.14159)
261 raises(0, 2.71828)
262 # Zero and non-integer step
263 raises(0, 42, 0)
264 raises(0, 42, 3.14159)
265
Raymond Hettinger356a4592004-08-30 06:14:31 +0000266 def test_genrandbits(self):
267 # Verify ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000268 for k in range(1, 1000):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000269 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000270
271 # Verify all bits active
272 getbits = self.gen.getrandbits
273 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
274 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000275 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000276 cum |= getbits(span)
277 self.assertEqual(cum, 2**span-1)
278
279 # Verify argument checking
280 self.assertRaises(TypeError, self.gen.getrandbits)
281 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
282 self.assertRaises(ValueError, self.gen.getrandbits, 0)
283 self.assertRaises(ValueError, self.gen.getrandbits, -1)
284 self.assertRaises(TypeError, self.gen.getrandbits, 10.1)
285
286 def test_randbelow_logic(self, _log=log, int=int):
287 # check bitcount transition points: 2**i and 2**(i+1)-1
288 # show that: k = int(1.001 + _log(n, 2))
289 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000290 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000291 n = 1 << i # check an exact power of two
Raymond Hettinger356a4592004-08-30 06:14:31 +0000292 numbits = i+1
293 k = int(1.00001 + _log(n, 2))
294 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000295 self.assertEqual(n, 2**(k-1))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000296
297 n += n - 1 # check 1 below the next power of two
298 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000299 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000300 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000301
302 n -= n >> 15 # check a little farther below the next power of two
303 k = int(1.00001 + _log(n, 2))
304 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000305 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger356a4592004-08-30 06:14:31 +0000306
307
Raymond Hettinger40f62172002-12-29 23:03:38 +0000308class MersenneTwister_TestBasicOps(TestBasicOps):
309 gen = random.Random()
310
Raymond Hettingerf763a722010-09-07 00:38:15 +0000311 def test_guaranteed_stable(self):
312 # These sequences are guaranteed to stay the same across versions of python
313 self.gen.seed(3456147, version=1)
314 self.assertEqual([self.gen.random().hex() for i in range(4)],
315 ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1',
316 '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000317 self.gen.seed("the quick brown fox", version=2)
318 self.assertEqual([self.gen.random().hex() for i in range(4)],
Raymond Hettinger3fcf0022010-12-08 01:13:53 +0000319 ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4',
320 '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000321
Raymond Hettinger58335872004-07-09 14:26:18 +0000322 def test_setstate_first_arg(self):
323 self.assertRaises(ValueError, self.gen.setstate, (1, None, None))
324
325 def test_setstate_middle_arg(self):
326 # Wrong type, s/b tuple
327 self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
328 # Wrong length, s/b 625
329 self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None))
330 # Wrong type, s/b tuple of 625 ints
331 self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
332 # Last element s/b an int also
333 self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))
334
R David Murraye3e1c172013-04-02 12:47:23 -0400335 # Little trick to make "tuple(x % (2**32) for x in internalstate)"
336 # raise ValueError. I cannot think of a simple way to achieve this, so
337 # I am opting for using a generator as the middle argument of setstate
338 # which attempts to cast a NaN to integer.
339 state_values = self.gen.getstate()[1]
340 state_values = list(state_values)
341 state_values[-1] = float('nan')
342 state = (int(x) for x in state_values)
343 self.assertRaises(TypeError, self.gen.setstate, (2, state, None))
344
Raymond Hettinger40f62172002-12-29 23:03:38 +0000345 def test_referenceImplementation(self):
346 # Compare the python implementation with results from the original
347 # code. Create 2000 53-bit precision random floats. Compare only
348 # the last ten entries to show that the independent implementations
349 # are tracking. Here is the main() function needed to create the
350 # list of expected random numbers:
351 # void main(void){
352 # int i;
353 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
354 # init_by_array(init, length);
355 # for (i=0; i<2000; i++) {
356 # printf("%.15f ", genrand_res53());
357 # if (i%5==4) printf("\n");
358 # }
359 # }
360 expected = [0.45839803073713259,
361 0.86057815201978782,
362 0.92848331726782152,
363 0.35932681119782461,
364 0.081823493762449573,
365 0.14332226470169329,
366 0.084297823823520024,
367 0.53814864671831453,
368 0.089215024911993401,
369 0.78486196105372907]
370
Guido van Rossume2a383d2007-01-15 16:59:06 +0000371 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000372 actual = self.randomlist(2000)[-10:]
373 for a, e in zip(actual, expected):
374 self.assertAlmostEqual(a,e,places=14)
375
376 def test_strong_reference_implementation(self):
377 # Like test_referenceImplementation, but checks for exact bit-level
378 # equality. This should pass on any box where C double contains
379 # at least 53 bits of precision (the underlying algorithm suffers
380 # no rounding errors -- all results are exact).
381 from math import ldexp
382
Guido van Rossume2a383d2007-01-15 16:59:06 +0000383 expected = [0x0eab3258d2231f,
384 0x1b89db315277a5,
385 0x1db622a5518016,
386 0x0b7f9af0d575bf,
387 0x029e4c4db82240,
388 0x04961892f5d673,
389 0x02b291598e4589,
390 0x11388382c15694,
391 0x02dad977c9e1fe,
392 0x191d96d4d334c6]
393 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000394 actual = self.randomlist(2000)[-10:]
395 for a, e in zip(actual, expected):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000396 self.assertEqual(int(ldexp(a, 53)), e)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000397
398 def test_long_seed(self):
399 # This is most interesting to run in debug mode, just to make sure
400 # nothing blows up. Under the covers, a dynamically resized array
401 # is allocated, consuming space proportional to the number of bits
402 # in the seed. Unfortunately, that's a quadratic-time algorithm,
403 # so don't make this horribly big.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000404 seed = (1 << (10000 * 8)) - 1 # about 10K bytes
Raymond Hettinger40f62172002-12-29 23:03:38 +0000405 self.gen.seed(seed)
406
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000407 def test_53_bits_per_float(self):
408 # This should pass whenever a C double has 53 bit precision.
409 span = 2 ** 53
410 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000411 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000412 cum |= int(self.gen.random() * span)
413 self.assertEqual(cum, span-1)
414
415 def test_bigrand(self):
416 # The randrange routine should build-up the required number of bits
417 # in stages so that all bit positions are active.
418 span = 2 ** 500
419 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000420 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000421 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000422 self.assertTrue(0 <= r < span)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000423 cum |= r
424 self.assertEqual(cum, span-1)
425
426 def test_bigrand_ranges(self):
427 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
428 start = self.gen.randrange(2 ** i)
429 stop = self.gen.randrange(2 ** (i-2))
430 if stop <= start:
431 return
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000432 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000433
434 def test_rangelimits(self):
435 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
Raymond Hettingera690a992003-11-16 16:17:49 +0000436 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000437 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000438
439 def test_genrandbits(self):
440 # Verify cross-platform repeatability
441 self.gen.seed(1234567)
442 self.assertEqual(self.gen.getrandbits(100),
Guido van Rossume2a383d2007-01-15 16:59:06 +0000443 97904845777343510404718956115)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000444 # Verify ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000445 for k in range(1, 1000):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000446 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000447
448 # Verify all bits active
449 getbits = self.gen.getrandbits
450 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
451 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000452 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000453 cum |= getbits(span)
454 self.assertEqual(cum, 2**span-1)
455
Raymond Hettinger58335872004-07-09 14:26:18 +0000456 # Verify argument checking
457 self.assertRaises(TypeError, self.gen.getrandbits)
458 self.assertRaises(TypeError, self.gen.getrandbits, 'a')
459 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
460 self.assertRaises(ValueError, self.gen.getrandbits, 0)
461 self.assertRaises(ValueError, self.gen.getrandbits, -1)
462
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000463 def test_randbelow_logic(self, _log=log, int=int):
464 # check bitcount transition points: 2**i and 2**(i+1)-1
465 # show that: k = int(1.001 + _log(n, 2))
466 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000467 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000468 n = 1 << i # check an exact power of two
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000469 numbits = i+1
470 k = int(1.00001 + _log(n, 2))
471 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000472 self.assertEqual(n, 2**(k-1))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000473
474 n += n - 1 # check 1 below the next power of two
475 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000476 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000477 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000478
479 n -= n >> 15 # check a little farther below the next power of two
480 k = int(1.00001 + _log(n, 2))
481 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000482 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000483
R David Murraye3e1c172013-04-02 12:47:23 -0400484 @unittest.mock.patch('random.Random.random')
485 def test_randbelow_overriden_random(self, random_mock):
486 # Random._randbelow() can only use random() when the built-in one
487 # has been overridden but no new getrandbits() method was supplied.
488 random_mock.side_effect = random.SystemRandom().random
489 maxsize = 1<<random.BPF
490 with warnings.catch_warnings():
491 warnings.simplefilter("ignore", UserWarning)
492 # Population range too large (n >= maxsize)
493 self.gen._randbelow(maxsize+1, maxsize = maxsize)
494 self.gen._randbelow(5640, maxsize = maxsize)
495
496 # This might be going too far to test a single line, but because of our
497 # noble aim of achieving 100% test coverage we need to write a case in
498 # which the following line in Random._randbelow() gets executed:
499 #
500 # rem = maxsize % n
501 # limit = (maxsize - rem) / maxsize
502 # r = random()
503 # while r >= limit:
504 # r = random() # <== *This line* <==<
505 #
506 # Therefore, to guarantee that the while loop is executed at least
507 # once, we need to mock random() so that it returns a number greater
508 # than 'limit' the first time it gets called.
509
510 n = 42
511 epsilon = 0.01
512 limit = (maxsize - (maxsize % n)) / maxsize
513 random_mock.side_effect = [limit + epsilon, limit - epsilon]
514 self.gen._randbelow(n, maxsize = maxsize)
515
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000516 def test_randrange_bug_1590891(self):
517 start = 1000000000000
518 stop = -100000000000000000000
519 step = -200
520 x = self.gen.randrange(start, stop, step)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000521 self.assertTrue(stop < x <= start)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000522 self.assertEqual((x+stop)%step, 0)
523
Raymond Hettinger2d0c2562009-02-19 09:53:18 +0000524def gamma(z, sqrt2pi=(2.0*pi)**0.5):
525 # Reflection to right half of complex plane
526 if z < 0.5:
527 return pi / sin(pi*z) / gamma(1.0-z)
528 # Lanczos approximation with g=7
529 az = z + (7.0 - 0.5)
530 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([
531 0.9999999999995183,
532 676.5203681218835 / z,
533 -1259.139216722289 / (z+1.0),
534 771.3234287757674 / (z+2.0),
535 -176.6150291498386 / (z+3.0),
536 12.50734324009056 / (z+4.0),
537 -0.1385710331296526 / (z+5.0),
538 0.9934937113930748e-05 / (z+6.0),
539 0.1659470187408462e-06 / (z+7.0),
540 ])
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000541
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000542class TestDistributions(unittest.TestCase):
543 def test_zeroinputs(self):
544 # Verify that distributions can handle a series of zero inputs'
545 g = random.Random()
Guido van Rossum805365e2007-05-07 22:24:25 +0000546 x = [g.random() for i in range(50)] + [0.0]*5
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000547 g.random = x[:].pop; g.uniform(1,10)
548 g.random = x[:].pop; g.paretovariate(1.0)
549 g.random = x[:].pop; g.expovariate(1.0)
550 g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200551 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000552 g.random = x[:].pop; g.normalvariate(0.0, 1.0)
553 g.random = x[:].pop; g.gauss(0.0, 1.0)
554 g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
555 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
556 g.random = x[:].pop; g.gammavariate(0.01, 1.0)
557 g.random = x[:].pop; g.gammavariate(1.0, 1.0)
558 g.random = x[:].pop; g.gammavariate(200.0, 1.0)
559 g.random = x[:].pop; g.betavariate(3.0, 3.0)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000560 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000561
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000562 def test_avg_std(self):
563 # Use integration to test distribution average and standard deviation.
564 # Only works for distributions which do not consume variates in pairs
565 g = random.Random()
566 N = 5000
Guido van Rossum805365e2007-05-07 22:24:25 +0000567 x = [i/float(N) for i in range(1,N)]
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000568 for variate, args, mu, sigmasqrd in [
569 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
Christian Heimesfe337bf2008-03-23 21:54:12 +0000570 (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 +0000571 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200572 (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000573 (g.paretovariate, (5.0,), 5.0/(5.0-1),
574 5.0/((5.0-1)**2*(5.0-2))),
575 (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
576 gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]:
577 g.random = x[:].pop
578 y = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000579 for i in range(len(x)):
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000580 try:
581 y.append(variate(*args))
582 except IndexError:
583 pass
584 s1 = s2 = 0
585 for e in y:
586 s1 += e
587 s2 += (e - mu) ** 2
588 N = len(y)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200589 self.assertAlmostEqual(s1/N, mu, places=2,
590 msg='%s%r' % (variate.__name__, args))
591 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
592 msg='%s%r' % (variate.__name__, args))
593
594 def test_constant(self):
595 g = random.Random()
596 N = 100
597 for variate, args, expected in [
598 (g.uniform, (10.0, 10.0), 10.0),
599 (g.triangular, (10.0, 10.0), 10.0),
600 #(g.triangular, (10.0, 10.0, 10.0), 10.0),
601 (g.expovariate, (float('inf'),), 0.0),
602 (g.vonmisesvariate, (3.0, float('inf')), 3.0),
603 (g.gauss, (10.0, 0.0), 10.0),
604 (g.lognormvariate, (0.0, 0.0), 1.0),
605 (g.lognormvariate, (-float('inf'), 0.0), 0.0),
606 (g.normalvariate, (10.0, 0.0), 10.0),
607 (g.paretovariate, (float('inf'),), 1.0),
608 (g.weibullvariate, (10.0, float('inf')), 10.0),
609 (g.weibullvariate, (0.0, 10.0), 0.0),
610 ]:
611 for i in range(N):
612 self.assertEqual(variate(*args), expected)
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000613
Mark Dickinsonbe5f9192013-02-10 14:16:10 +0000614 def test_von_mises_range(self):
615 # Issue 17149: von mises variates were not consistently in the
616 # range [0, 2*PI].
617 g = random.Random()
618 N = 100
619 for mu in 0.0, 0.1, 3.1, 6.2:
620 for kappa in 0.0, 2.3, 500.0:
621 for _ in range(N):
622 sample = g.vonmisesvariate(mu, kappa)
623 self.assertTrue(
624 0 <= sample <= random.TWOPI,
625 msg=("vonmisesvariate({}, {}) produced a result {} out"
626 " of range [0, 2*pi]").format(mu, kappa, sample))
627
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200628 def test_von_mises_large_kappa(self):
629 # Issue #17141: vonmisesvariate() was hang for large kappas
630 random.vonmisesvariate(0, 1e15)
631 random.vonmisesvariate(0, 1e100)
632
R David Murraye3e1c172013-04-02 12:47:23 -0400633 def test_gammavariate_errors(self):
634 # Both alpha and beta must be > 0.0
635 self.assertRaises(ValueError, random.gammavariate, -1, 3)
636 self.assertRaises(ValueError, random.gammavariate, 0, 2)
637 self.assertRaises(ValueError, random.gammavariate, 2, 0)
638 self.assertRaises(ValueError, random.gammavariate, 1, -3)
639
640 @unittest.mock.patch('random.Random.random')
641 def test_gammavariate_full_code_coverage(self, random_mock):
642 # There are three different possibilities in the current implementation
643 # of random.gammavariate(), depending on the value of 'alpha'. What we
644 # are going to do here is to fix the values returned by random() to
645 # generate test cases that provide 100% line coverage of the method.
646
647 # #1: alpha > 1.0: we want the first random number to be outside the
648 # [1e-7, .9999999] range, so that the continue statement executes
649 # once. The values of u1 and u2 will be 0.5 and 0.3, respectively.
650 random_mock.side_effect = [1e-8, 0.5, 0.3]
651 returned_value = random.gammavariate(1.1, 2.3)
652 self.assertAlmostEqual(returned_value, 2.53)
653
654 # #2: alpha == 1: first random number less than 1e-7 to that the body
655 # of the while loop executes once. Then random.random() returns 0.45,
656 # which causes while to stop looping and the algorithm to terminate.
657 random_mock.side_effect = [1e-8, 0.45]
658 returned_value = random.gammavariate(1.0, 3.14)
659 self.assertAlmostEqual(returned_value, 2.507314166123803)
660
661 # #3: 0 < alpha < 1. This is the most complex region of code to cover,
662 # as there are multiple if-else statements. Let's take a look at the
663 # source code, and determine the values that we need accordingly:
664 #
665 # while 1:
666 # u = random()
667 # b = (_e + alpha)/_e
668 # p = b*u
669 # if p <= 1.0: # <=== (A)
670 # x = p ** (1.0/alpha)
671 # else: # <=== (B)
672 # x = -_log((b-p)/alpha)
673 # u1 = random()
674 # if p > 1.0: # <=== (C)
675 # if u1 <= x ** (alpha - 1.0): # <=== (D)
676 # break
677 # elif u1 <= _exp(-x): # <=== (E)
678 # break
679 # return x * beta
680 #
681 # First, we want (A) to be True. For that we need that:
682 # b*random() <= 1.0
683 # r1 = random() <= 1.0 / b
684 #
685 # We now get to the second if-else branch, and here, since p <= 1.0,
686 # (C) is False and we take the elif branch, (E). For it to be True,
687 # so that the break is executed, we need that:
688 # r2 = random() <= _exp(-x)
689 # r2 <= _exp(-(p ** (1.0/alpha)))
690 # r2 <= _exp(-((b*r1) ** (1.0/alpha)))
691
692 _e = random._e
693 _exp = random._exp
694 _log = random._log
695 alpha = 0.35
696 beta = 1.45
697 b = (_e + alpha)/_e
698 epsilon = 0.01
699
700 r1 = 0.8859296441566 # 1.0 / b
701 r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha)))
702
703 # These four "random" values result in the following trace:
704 # (A) True, (E) False --> [next iteration of while]
705 # (A) True, (E) True --> [while loop breaks]
706 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
707 returned_value = random.gammavariate(alpha, beta)
708 self.assertAlmostEqual(returned_value, 1.4499999999997544)
709
710 # Let's now make (A) be False. If this is the case, when we get to the
711 # second if-else 'p' is greater than 1, so (C) evaluates to True. We
712 # now encounter a second if statement, (D), which in order to execute
713 # must satisfy the following condition:
714 # r2 <= x ** (alpha - 1.0)
715 # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0)
716 # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0)
717 r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False
718 r2 = 0.9445400408898141
719
720 # And these four values result in the following trace:
721 # (B) and (C) True, (D) False --> [next iteration of while]
722 # (B) and (C) True, (D) True [while loop breaks]
723 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
724 returned_value = random.gammavariate(alpha, beta)
725 self.assertAlmostEqual(returned_value, 1.5830349561760781)
726
727 @unittest.mock.patch('random.Random.gammavariate')
728 def test_betavariate_return_zero(self, gammavariate_mock):
729 # betavariate() returns zero when the Gamma distribution
730 # that it uses internally returns this same value.
731 gammavariate_mock.return_value = 0.0
732 self.assertEqual(0.0, random.betavariate(2.71828, 3.14159))
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200733
Raymond Hettinger40f62172002-12-29 23:03:38 +0000734class TestModule(unittest.TestCase):
735 def testMagicConstants(self):
736 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
737 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
738 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
739 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
740
741 def test__all__(self):
742 # tests validity but not completeness of the __all__ list
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000743 self.assertTrue(set(random.__all__) <= set(dir(random)))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000744
Thomas Woutersb2137042007-02-01 18:02:27 +0000745 def test_random_subclass_with_kwargs(self):
746 # SF bug #1486663 -- this used to erroneously raise a TypeError
747 class Subclass(random.Random):
748 def __init__(self, newarg=None):
749 random.Random.__init__(self)
750 Subclass(newarg=1)
751
752
Raymond Hettinger105b0842003-02-04 05:47:30 +0000753def test_main(verbose=None):
Raymond Hettinger28de64f2008-01-13 23:40:30 +0000754 testclasses = [MersenneTwister_TestBasicOps,
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000755 TestDistributions,
Raymond Hettingerb8717632004-09-04 20:13:29 +0000756 TestModule]
757
Raymond Hettingerc1c43ca2004-09-05 00:00:42 +0000758 try:
Raymond Hettinger23f12412004-09-13 22:23:21 +0000759 random.SystemRandom().random()
Raymond Hettingerc1c43ca2004-09-05 00:00:42 +0000760 except NotImplementedError:
761 pass
762 else:
Raymond Hettinger23f12412004-09-13 22:23:21 +0000763 testclasses.append(SystemRandom_TestBasicOps)
Raymond Hettingerb8717632004-09-04 20:13:29 +0000764
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000765 support.run_unittest(*testclasses)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000766
Raymond Hettinger105b0842003-02-04 05:47:30 +0000767 # verify reference counting
768 import sys
769 if verbose and hasattr(sys, "gettotalrefcount"):
Raymond Hettinger320a1b02003-05-02 22:44:59 +0000770 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000771 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000772 support.run_unittest(*testclasses)
Raymond Hettinger320a1b02003-05-02 22:44:59 +0000773 counts[i] = sys.gettotalrefcount()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000774 print(counts)
Raymond Hettinger105b0842003-02-04 05:47:30 +0000775
Raymond Hettinger40f62172002-12-29 23:03:38 +0000776if __name__ == "__main__":
Raymond Hettinger105b0842003-02-04 05:47:30 +0000777 test_main(verbose=True)