blob: a80e71e67e4c6ce037f6f1ed38f109198e8aa0e7 [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
Antoine Pitrou346cbd32017-05-27 17:50:54 +02004import os
Raymond Hettinger40f62172002-12-29 23:03:38 +00005import time
Raymond Hettinger5f078ff2003-06-24 20:29:04 +00006import pickle
Raymond Hettinger2f726e92003-10-05 09:09:15 +00007import warnings
Dong-hee Na814b07b2020-06-21 19:33:06 +09008import test.support
9
R David Murraye3e1c172013-04-02 12:47:23 -040010from functools import partial
Victor Stinnerbd1b49a2016-10-19 10:11:37 +020011from math import log, exp, pi, fsum, sin, factorial
Benjamin Petersonee8712c2008-05-20 21:35:26 +000012from test import support
Raymond Hettingere8f1e002016-09-06 17:15:29 -070013from fractions import Fraction
Raymond Hettinger81a5fc32020-05-08 07:53:15 -070014from collections import Counter
csabellaf111fd22017-05-11 11:19:35 -040015
Ezio Melotti3e4a98b2013-04-19 05:45:27 +030016class TestBasicOps:
Raymond Hettinger40f62172002-12-29 23:03:38 +000017 # Superclass with tests common to all generators.
18 # Subclasses must arrange for self.gen to retrieve the Random instance
19 # to be tested.
Tim Peters46c04e12002-05-05 20:40:00 +000020
Raymond Hettinger40f62172002-12-29 23:03:38 +000021 def randomlist(self, n):
22 """Helper function to make a list of random numbers"""
Guido van Rossum805365e2007-05-07 22:24:25 +000023 return [self.gen.random() for i in range(n)]
Tim Peters46c04e12002-05-05 20:40:00 +000024
Raymond Hettinger40f62172002-12-29 23:03:38 +000025 def test_autoseed(self):
26 self.gen.seed()
27 state1 = self.gen.getstate()
Raymond Hettinger3081d592003-08-09 18:30:57 +000028 time.sleep(0.1)
Mike53f7a7c2017-12-14 14:04:53 +030029 self.gen.seed() # different seeds at different times
Raymond Hettinger40f62172002-12-29 23:03:38 +000030 state2 = self.gen.getstate()
31 self.assertNotEqual(state1, state2)
Tim Peters46c04e12002-05-05 20:40:00 +000032
Raymond Hettinger40f62172002-12-29 23:03:38 +000033 def test_saverestore(self):
34 N = 1000
35 self.gen.seed()
36 state = self.gen.getstate()
37 randseq = self.randomlist(N)
38 self.gen.setstate(state) # should regenerate the same sequence
39 self.assertEqual(randseq, self.randomlist(N))
40
41 def test_seedargs(self):
Mark Dickinson95aeae02012-06-24 11:05:30 +010042 # Seed value with a negative hash.
43 class MySeed(object):
44 def __hash__(self):
45 return -1729
Xtreaka06d6832019-09-12 09:13:20 +010046 for arg in [None, 0, 1, -1, 10**20, -(10**20),
Victor Stinner00d7cd82020-03-10 15:15:14 +010047 False, True, 3.14, 'a']:
Raymond Hettinger40f62172002-12-29 23:03:38 +000048 self.gen.seed(arg)
Xtreaka06d6832019-09-12 09:13:20 +010049
50 for arg in [1+2j, tuple('abc'), MySeed()]:
51 with self.assertWarns(DeprecationWarning):
52 self.gen.seed(arg)
53
Guido van Rossum805365e2007-05-07 22:24:25 +000054 for arg in [list(range(3)), dict(one=1)]:
Xtreaka06d6832019-09-12 09:13:20 +010055 with self.assertWarns(DeprecationWarning):
56 self.assertRaises(TypeError, self.gen.seed, arg)
Raymond Hettingerf763a722010-09-07 00:38:15 +000057 self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
Raymond Hettinger58335872004-07-09 14:26:18 +000058 self.assertRaises(TypeError, type(self.gen), [])
Raymond Hettinger40f62172002-12-29 23:03:38 +000059
R David Murraye3e1c172013-04-02 12:47:23 -040060 @unittest.mock.patch('random._urandom') # os.urandom
61 def test_seed_when_randomness_source_not_found(self, urandom_mock):
62 # Random.seed() uses time.time() when an operating system specific
csabellaf111fd22017-05-11 11:19:35 -040063 # randomness source is not found. To test this on machines where it
R David Murraye3e1c172013-04-02 12:47:23 -040064 # exists, run the above test, test_seedargs(), again after mocking
65 # os.urandom() so that it raises the exception expected when the
66 # randomness source is not available.
67 urandom_mock.side_effect = NotImplementedError
68 self.test_seedargs()
69
Antoine Pitrou5e394332012-11-04 02:10:33 +010070 def test_shuffle(self):
71 shuffle = self.gen.shuffle
72 lst = []
73 shuffle(lst)
74 self.assertEqual(lst, [])
75 lst = [37]
76 shuffle(lst)
77 self.assertEqual(lst, [37])
78 seqs = [list(range(n)) for n in range(10)]
79 shuffled_seqs = [list(range(n)) for n in range(10)]
80 for shuffled_seq in shuffled_seqs:
81 shuffle(shuffled_seq)
82 for (seq, shuffled_seq) in zip(seqs, shuffled_seqs):
83 self.assertEqual(len(seq), len(shuffled_seq))
84 self.assertEqual(set(seq), set(shuffled_seq))
Antoine Pitrou5e394332012-11-04 02:10:33 +010085 # The above tests all would pass if the shuffle was a
86 # no-op. The following non-deterministic test covers that. It
87 # asserts that the shuffled sequence of 1000 distinct elements
88 # must be different from the original one. Although there is
89 # mathematically a non-zero probability that this could
90 # actually happen in a genuinely random shuffle, it is
91 # completely negligible, given that the number of possible
92 # permutations of 1000 objects is 1000! (factorial of 1000),
93 # which is considerably larger than the number of atoms in the
94 # universe...
95 lst = list(range(1000))
96 shuffled_lst = list(range(1000))
97 shuffle(shuffled_lst)
98 self.assertTrue(lst != shuffled_lst)
99 shuffle(lst)
100 self.assertTrue(lst != shuffled_lst)
csabellaf111fd22017-05-11 11:19:35 -0400101 self.assertRaises(TypeError, shuffle, (1, 2, 3))
102
103 def test_shuffle_random_argument(self):
104 # Test random argument to shuffle.
105 shuffle = self.gen.shuffle
106 mock_random = unittest.mock.Mock(return_value=0.5)
107 seq = bytearray(b'abcdefghijk')
Raymond Hettinger190fac92020-05-02 16:45:32 -0700108 with self.assertWarns(DeprecationWarning):
109 shuffle(seq, mock_random)
csabellaf111fd22017-05-11 11:19:35 -0400110 mock_random.assert_called_with()
Antoine Pitrou5e394332012-11-04 02:10:33 +0100111
Raymond Hettingerdc4872e2010-09-07 10:06:56 +0000112 def test_choice(self):
113 choice = self.gen.choice
114 with self.assertRaises(IndexError):
115 choice([])
116 self.assertEqual(choice([50]), 50)
117 self.assertIn(choice([25, 75]), [25, 75])
118
Raymond Hettinger40f62172002-12-29 23:03:38 +0000119 def test_sample(self):
120 # For the entire allowable range of 0 <= k <= N, validate that
121 # the sample is of the correct length and contains only unique items
122 N = 100
Guido van Rossum805365e2007-05-07 22:24:25 +0000123 population = range(N)
124 for k in range(N+1):
Raymond Hettinger40f62172002-12-29 23:03:38 +0000125 s = self.gen.sample(population, k)
126 self.assertEqual(len(s), k)
Raymond Hettingera690a992003-11-16 16:17:49 +0000127 uniq = set(s)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000128 self.assertEqual(len(uniq), k)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000129 self.assertTrue(uniq <= set(population))
Raymond Hettinger8ec78812003-01-04 05:55:11 +0000130 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
R David Murraye3e1c172013-04-02 12:47:23 -0400131 # Exception raised if size of sample exceeds that of population
132 self.assertRaises(ValueError, self.gen.sample, population, N+1)
Raymond Hettingerbf871262016-11-21 14:34:33 -0800133 self.assertRaises(ValueError, self.gen.sample, [], -1)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000134
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000135 def test_sample_distribution(self):
136 # For the entire allowable range of 0 <= k <= N, validate that
137 # sample generates all possible permutations
138 n = 5
139 pop = range(n)
140 trials = 10000 # large num prevents false negatives without slowing normal case
Guido van Rossum805365e2007-05-07 22:24:25 +0000141 for k in range(n):
Raymond Hettingerffdb8bb2004-09-27 15:29:05 +0000142 expected = factorial(n) // factorial(n-k)
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000143 perms = {}
Guido van Rossum805365e2007-05-07 22:24:25 +0000144 for i in range(trials):
Raymond Hettinger7b0cf762003-01-17 17:23:23 +0000145 perms[tuple(self.gen.sample(pop, k))] = None
146 if len(perms) == expected:
147 break
148 else:
149 self.fail()
150
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000151 def test_sample_inputs(self):
152 # SF bug #801342 -- population can be any iterable defining __len__()
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000153 self.gen.sample(range(20), 2)
Guido van Rossum805365e2007-05-07 22:24:25 +0000154 self.gen.sample(range(20), 2)
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000155 self.gen.sample(str('abcdefghijklmnopqrst'), 2)
156 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
157
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 def test_sample_on_dicts(self):
Raymond Hettinger1acde192008-01-14 01:00:53 +0000159 self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160
Raymond Hettinger4fe00202020-04-19 00:36:42 -0700161 def test_sample_on_sets(self):
162 with self.assertWarns(DeprecationWarning):
163 population = {10, 20, 30, 40, 50, 60, 70}
164 self.gen.sample(population, k=5)
165
Raymond Hettinger81a5fc32020-05-08 07:53:15 -0700166 def test_sample_with_counts(self):
167 sample = self.gen.sample
168
169 # General case
170 colors = ['red', 'green', 'blue', 'orange', 'black', 'brown', 'amber']
171 counts = [500, 200, 20, 10, 5, 0, 1 ]
172 k = 700
173 summary = Counter(sample(colors, counts=counts, k=k))
174 self.assertEqual(sum(summary.values()), k)
175 for color, weight in zip(colors, counts):
176 self.assertLessEqual(summary[color], weight)
177 self.assertNotIn('brown', summary)
178
179 # Case that exhausts the population
180 k = sum(counts)
181 summary = Counter(sample(colors, counts=counts, k=k))
182 self.assertEqual(sum(summary.values()), k)
183 for color, weight in zip(colors, counts):
184 self.assertLessEqual(summary[color], weight)
185 self.assertNotIn('brown', summary)
186
187 # Case with population size of 1
188 summary = Counter(sample(['x'], counts=[10], k=8))
189 self.assertEqual(summary, Counter(x=8))
190
191 # Case with all counts equal.
192 nc = len(colors)
193 summary = Counter(sample(colors, counts=[10]*nc, k=10*nc))
194 self.assertEqual(summary, Counter(10*colors))
195
196 # Test error handling
197 with self.assertRaises(TypeError):
198 sample(['red', 'green', 'blue'], counts=10, k=10) # counts not iterable
199 with self.assertRaises(ValueError):
200 sample(['red', 'green', 'blue'], counts=[-3, -7, -8], k=2) # counts are negative
201 with self.assertRaises(ValueError):
202 sample(['red', 'green', 'blue'], counts=[0, 0, 0], k=2) # counts are zero
203 with self.assertRaises(ValueError):
204 sample(['red', 'green'], counts=[10, 10], k=21) # population too small
205 with self.assertRaises(ValueError):
206 sample(['red', 'green', 'blue'], counts=[1, 2], k=2) # too few counts
207 with self.assertRaises(ValueError):
208 sample(['red', 'green', 'blue'], counts=[1, 2, 3, 4], k=2) # too many counts
209
210 def test_sample_counts_equivalence(self):
211 # Test the documented strong equivalence to a sample with repeated elements.
212 # We run this test on random.Random() which makes deterministic selections
213 # for a given seed value.
214 sample = random.sample
215 seed = random.seed
216
217 colors = ['red', 'green', 'blue', 'orange', 'black', 'amber']
218 counts = [500, 200, 20, 10, 5, 1 ]
219 k = 700
220 seed(8675309)
221 s1 = sample(colors, counts=counts, k=k)
222 seed(8675309)
223 expanded = [color for (color, count) in zip(colors, counts) for i in range(count)]
224 self.assertEqual(len(expanded), sum(counts))
225 s2 = sample(expanded, k=k)
226 self.assertEqual(s1, s2)
227
228 pop = 'abcdefghi'
229 counts = [10, 9, 8, 7, 6, 5, 4, 3, 2]
230 seed(8675309)
231 s1 = ''.join(sample(pop, counts=counts, k=30))
232 expanded = ''.join([letter for (letter, count) in zip(pop, counts) for i in range(count)])
233 seed(8675309)
234 s2 = ''.join(sample(expanded, k=30))
235 self.assertEqual(s1, s2)
236
Raymond Hettinger28aa4a02016-09-07 00:08:44 -0700237 def test_choices(self):
238 choices = self.gen.choices
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700239 data = ['red', 'green', 'blue', 'yellow']
240 str_data = 'abcd'
241 range_data = range(4)
242 set_data = set(range(4))
243
244 # basic functionality
245 for sample in [
Raymond Hettinger9016f282016-09-26 21:45:57 -0700246 choices(data, k=5),
247 choices(data, range(4), k=5),
Raymond Hettinger28aa4a02016-09-07 00:08:44 -0700248 choices(k=5, population=data, weights=range(4)),
249 choices(k=5, population=data, cum_weights=range(4)),
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700250 ]:
251 self.assertEqual(len(sample), 5)
252 self.assertEqual(type(sample), list)
253 self.assertTrue(set(sample) <= set(data))
254
255 # test argument handling
Raymond Hettinger28aa4a02016-09-07 00:08:44 -0700256 with self.assertRaises(TypeError): # missing arguments
257 choices(2)
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700258
Raymond Hettinger9016f282016-09-26 21:45:57 -0700259 self.assertEqual(choices(data, k=0), []) # k == 0
260 self.assertEqual(choices(data, k=-1), []) # negative k behaves like ``[0] * -1``
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700261 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700262 choices(data, k=2.5) # k is a float
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700263
Raymond Hettinger9016f282016-09-26 21:45:57 -0700264 self.assertTrue(set(choices(str_data, k=5)) <= set(str_data)) # population is a string sequence
265 self.assertTrue(set(choices(range_data, k=5)) <= set(range_data)) # population is a range
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700266 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700267 choices(set_data, k=2) # population is not a sequence
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700268
Raymond Hettinger9016f282016-09-26 21:45:57 -0700269 self.assertTrue(set(choices(data, None, k=5)) <= set(data)) # weights is None
270 self.assertTrue(set(choices(data, weights=None, k=5)) <= set(data))
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700271 with self.assertRaises(ValueError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700272 choices(data, [1,2], k=5) # len(weights) != len(population)
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700273 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700274 choices(data, 10, k=5) # non-iterable weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700275 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700276 choices(data, [None]*4, k=5) # non-numeric weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700277 for weights in [
278 [15, 10, 25, 30], # integer weights
279 [15.1, 10.2, 25.2, 30.3], # float weights
280 [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights
281 [True, False, True, False] # booleans (include / exclude)
282 ]:
Raymond Hettinger9016f282016-09-26 21:45:57 -0700283 self.assertTrue(set(choices(data, weights, k=5)) <= set(data))
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700284
285 with self.assertRaises(ValueError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700286 choices(data, cum_weights=[1,2], k=5) # len(weights) != len(population)
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700287 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700288 choices(data, cum_weights=10, k=5) # non-iterable cum_weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700289 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700290 choices(data, cum_weights=[None]*4, k=5) # non-numeric cum_weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700291 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700292 choices(data, range(4), cum_weights=range(4), k=5) # both weights and cum_weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700293 for weights in [
294 [15, 10, 25, 30], # integer cum_weights
295 [15.1, 10.2, 25.2, 30.3], # float cum_weights
296 [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional cum_weights
297 ]:
Raymond Hettinger9016f282016-09-26 21:45:57 -0700298 self.assertTrue(set(choices(data, cum_weights=weights, k=5)) <= set(data))
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700299
Raymond Hettinger7b166522016-10-14 01:19:38 -0400300 # Test weight focused on a single element of the population
301 self.assertEqual(choices('abcd', [1, 0, 0, 0]), ['a'])
302 self.assertEqual(choices('abcd', [0, 1, 0, 0]), ['b'])
303 self.assertEqual(choices('abcd', [0, 0, 1, 0]), ['c'])
304 self.assertEqual(choices('abcd', [0, 0, 0, 1]), ['d'])
305
306 # Test consistency with random.choice() for empty population
307 with self.assertRaises(IndexError):
308 choices([], k=1)
309 with self.assertRaises(IndexError):
310 choices([], weights=[], k=1)
311 with self.assertRaises(IndexError):
312 choices([], cum_weights=[], k=5)
313
Raymond Hettingerddf71712018-06-27 01:08:31 -0700314 def test_choices_subnormal(self):
Min ho Kim96e12d52019-07-22 06:12:33 +1000315 # Subnormal weights would occasionally trigger an IndexError
Raymond Hettingerddf71712018-06-27 01:08:31 -0700316 # in choices() when the value returned by random() was large
317 # enough to make `random() * total` round up to the total.
318 # See https://bugs.python.org/msg275594 for more detail.
319 choices = self.gen.choices
320 choices(population=[1, 2], weights=[1e-323, 1e-323], k=5000)
321
Raymond Hettinger041d8b42019-11-23 02:22:13 -0800322 def test_choices_with_all_zero_weights(self):
323 # See issue #38881
324 with self.assertRaises(ValueError):
325 self.gen.choices('AB', [0.0, 0.0])
326
Raymond Hettinger40f62172002-12-29 23:03:38 +0000327 def test_gauss(self):
328 # Ensure that the seed() method initializes all the hidden state. In
329 # particular, through 2.2.1 it failed to reset a piece of state used
330 # by (and only by) the .gauss() method.
331
332 for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
333 self.gen.seed(seed)
334 x1 = self.gen.random()
335 y1 = self.gen.gauss(0, 1)
336
337 self.gen.seed(seed)
338 x2 = self.gen.random()
339 y2 = self.gen.gauss(0, 1)
340
341 self.assertEqual(x1, x2)
342 self.assertEqual(y1, y2)
343
Antoine Pitrou75a33782020-04-17 19:32:14 +0200344 def test_getrandbits(self):
345 # Verify ranges
346 for k in range(1, 1000):
347 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
348 self.assertEqual(self.gen.getrandbits(0), 0)
349
350 # Verify all bits active
351 getbits = self.gen.getrandbits
352 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
353 all_bits = 2**span-1
354 cum = 0
355 cpl_cum = 0
356 for i in range(100):
357 v = getbits(span)
358 cum |= v
359 cpl_cum |= all_bits ^ v
360 self.assertEqual(cum, all_bits)
361 self.assertEqual(cpl_cum, all_bits)
362
363 # Verify argument checking
364 self.assertRaises(TypeError, self.gen.getrandbits)
365 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
366 self.assertRaises(ValueError, self.gen.getrandbits, -1)
367 self.assertRaises(TypeError, self.gen.getrandbits, 10.1)
368
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000369 def test_pickling(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +0200370 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
371 state = pickle.dumps(self.gen, proto)
372 origseq = [self.gen.random() for i in range(10)]
373 newgen = pickle.loads(state)
374 restoredseq = [newgen.random() for i in range(10)]
375 self.assertEqual(origseq, restoredseq)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000376
Dong-hee Na814b07b2020-06-21 19:33:06 +0900377 @test.support.cpython_only
378 def test_bug_41052(self):
379 # _random.Random should not be allowed to serialization
380 import _random
381 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
382 r = _random.Random()
383 self.assertRaises(TypeError, pickle.dumps, r, proto)
384
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000385 def test_bug_1727780(self):
386 # verify that version-2-pickles can be loaded
387 # fine, whether they are created on 32-bit or 64-bit
388 # platforms, and that version-3-pickles load fine.
389 files = [("randv2_32.pck", 780),
390 ("randv2_64.pck", 866),
391 ("randv3.pck", 343)]
392 for file, value in files:
Serhiy Storchaka5b10b982019-03-05 10:06:26 +0200393 with open(support.findfile(file),"rb") as f:
394 r = pickle.load(f)
Raymond Hettinger05156612010-09-07 04:44:52 +0000395 self.assertEqual(int(r.random()*1000), value)
396
397 def test_bug_9025(self):
398 # Had problem with an uneven distribution in int(n*random())
399 # Verify the fix by checking that distributions fall within expectations.
400 n = 100000
401 randrange = self.gen.randrange
402 k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
403 self.assertTrue(0.30 < k/n < .37, (k/n))
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000404
Victor Stinner9f5fe792020-04-17 19:05:35 +0200405 def test_randbytes(self):
406 # Verify ranges
407 for n in range(1, 10):
408 data = self.gen.randbytes(n)
409 self.assertEqual(type(data), bytes)
410 self.assertEqual(len(data), n)
411
412 self.assertEqual(self.gen.randbytes(0), b'')
413
414 # Verify argument checking
415 self.assertRaises(TypeError, self.gen.randbytes)
416 self.assertRaises(TypeError, self.gen.randbytes, 1, 2)
417 self.assertRaises(ValueError, self.gen.randbytes, -1)
418 self.assertRaises(TypeError, self.gen.randbytes, 1.0)
419
420
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300421try:
422 random.SystemRandom().random()
423except NotImplementedError:
424 SystemRandom_available = False
425else:
426 SystemRandom_available = True
427
428@unittest.skipUnless(SystemRandom_available, "random.SystemRandom not available")
429class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
Raymond Hettinger23f12412004-09-13 22:23:21 +0000430 gen = random.SystemRandom()
Raymond Hettinger356a4592004-08-30 06:14:31 +0000431
432 def test_autoseed(self):
433 # Doesn't need to do anything except not fail
434 self.gen.seed()
435
436 def test_saverestore(self):
437 self.assertRaises(NotImplementedError, self.gen.getstate)
438 self.assertRaises(NotImplementedError, self.gen.setstate, None)
439
440 def test_seedargs(self):
441 # Doesn't need to do anything except not fail
442 self.gen.seed(100)
443
Raymond Hettinger356a4592004-08-30 06:14:31 +0000444 def test_gauss(self):
445 self.gen.gauss_next = None
446 self.gen.seed(100)
447 self.assertEqual(self.gen.gauss_next, None)
448
449 def test_pickling(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +0200450 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
451 self.assertRaises(NotImplementedError, pickle.dumps, self.gen, proto)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000452
453 def test_53_bits_per_float(self):
454 # This should pass whenever a C double has 53 bit precision.
455 span = 2 ** 53
456 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000457 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000458 cum |= int(self.gen.random() * span)
459 self.assertEqual(cum, span-1)
460
461 def test_bigrand(self):
462 # The randrange routine should build-up the required number of bits
463 # in stages so that all bit positions are active.
464 span = 2 ** 500
465 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000466 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000467 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000468 self.assertTrue(0 <= r < span)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000469 cum |= r
470 self.assertEqual(cum, span-1)
471
472 def test_bigrand_ranges(self):
473 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600474 start = self.gen.randrange(2 ** (i-2))
475 stop = self.gen.randrange(2 ** i)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000476 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600477 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000478 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000479
480 def test_rangelimits(self):
481 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
482 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000483 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000484
R David Murraye3e1c172013-04-02 12:47:23 -0400485 def test_randrange_nonunit_step(self):
486 rint = self.gen.randrange(0, 10, 2)
487 self.assertIn(rint, (0, 2, 4, 6, 8))
488 rint = self.gen.randrange(0, 2, 2)
489 self.assertEqual(rint, 0)
490
491 def test_randrange_errors(self):
492 raises = partial(self.assertRaises, ValueError, self.gen.randrange)
493 # Empty range
494 raises(3, 3)
495 raises(-721)
496 raises(0, 100, -12)
497 # Non-integer start/stop
498 raises(3.14159)
499 raises(0, 2.71828)
500 # Zero and non-integer step
501 raises(0, 42, 0)
502 raises(0, 42, 3.14159)
503
Raymond Hettinger356a4592004-08-30 06:14:31 +0000504 def test_randbelow_logic(self, _log=log, int=int):
505 # check bitcount transition points: 2**i and 2**(i+1)-1
506 # show that: k = int(1.001 + _log(n, 2))
507 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000508 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000509 n = 1 << i # check an exact power of two
Raymond Hettinger356a4592004-08-30 06:14:31 +0000510 numbits = i+1
511 k = int(1.00001 + _log(n, 2))
512 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000513 self.assertEqual(n, 2**(k-1))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000514
515 n += n - 1 # check 1 below the next power of two
516 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000517 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000518 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000519
520 n -= n >> 15 # check a little farther below the next power of two
521 k = int(1.00001 + _log(n, 2))
522 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000523 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger356a4592004-08-30 06:14:31 +0000524
525
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300526class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
Raymond Hettinger40f62172002-12-29 23:03:38 +0000527 gen = random.Random()
528
Raymond Hettingerf763a722010-09-07 00:38:15 +0000529 def test_guaranteed_stable(self):
530 # These sequences are guaranteed to stay the same across versions of python
531 self.gen.seed(3456147, version=1)
532 self.assertEqual([self.gen.random().hex() for i in range(4)],
533 ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1',
534 '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000535 self.gen.seed("the quick brown fox", version=2)
536 self.assertEqual([self.gen.random().hex() for i in range(4)],
Raymond Hettinger3fcf0022010-12-08 01:13:53 +0000537 ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4',
538 '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000539
Raymond Hettingerc7bab7c2016-08-31 15:01:08 -0700540 def test_bug_27706(self):
541 # Verify that version 1 seeds are unaffected by hash randomization
542
543 self.gen.seed('nofar', version=1) # hash('nofar') == 5990528763808513177
544 self.assertEqual([self.gen.random().hex() for i in range(4)],
545 ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5',
546 '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6'])
547
548 self.gen.seed('rachel', version=1) # hash('rachel') == -9091735575445484789
549 self.assertEqual([self.gen.random().hex() for i in range(4)],
550 ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3',
551 '0x1.3052b9c072678p-2', '0x1.578f332106574p-3'])
552
553 self.gen.seed('', version=1) # hash('') == 0
554 self.assertEqual([self.gen.random().hex() for i in range(4)],
555 ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1',
556 '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2'])
557
Oren Milmand780b2d2017-09-28 10:50:01 +0300558 def test_bug_31478(self):
559 # There shouldn't be an assertion failure in _random.Random.seed() in
560 # case the argument has a bad __abs__() method.
561 class BadInt(int):
562 def __abs__(self):
563 return None
564 try:
565 self.gen.seed(BadInt())
566 except TypeError:
567 pass
568
Raymond Hettinger132a7d72017-09-17 09:04:30 -0700569 def test_bug_31482(self):
570 # Verify that version 1 seeds are unaffected by hash randomization
571 # when the seeds are expressed as bytes rather than strings.
572 # The hash(b) values listed are the Python2.7 hash() values
573 # which were used for seeding.
574
575 self.gen.seed(b'nofar', version=1) # hash('nofar') == 5990528763808513177
576 self.assertEqual([self.gen.random().hex() for i in range(4)],
577 ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5',
578 '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6'])
579
580 self.gen.seed(b'rachel', version=1) # hash('rachel') == -9091735575445484789
581 self.assertEqual([self.gen.random().hex() for i in range(4)],
582 ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3',
583 '0x1.3052b9c072678p-2', '0x1.578f332106574p-3'])
584
585 self.gen.seed(b'', version=1) # hash('') == 0
586 self.assertEqual([self.gen.random().hex() for i in range(4)],
587 ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1',
588 '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2'])
589
590 b = b'\x00\x20\x40\x60\x80\xA0\xC0\xE0\xF0'
591 self.gen.seed(b, version=1) # hash(b) == 5015594239749365497
592 self.assertEqual([self.gen.random().hex() for i in range(4)],
593 ['0x1.52c2fde444d23p-1', '0x1.875174f0daea4p-2',
594 '0x1.9e9b2c50e5cd2p-1', '0x1.fa57768bd321cp-2'])
595
Raymond Hettinger58335872004-07-09 14:26:18 +0000596 def test_setstate_first_arg(self):
597 self.assertRaises(ValueError, self.gen.setstate, (1, None, None))
598
599 def test_setstate_middle_arg(self):
bladebryan9616a822017-04-21 23:10:46 -0700600 start_state = self.gen.getstate()
Raymond Hettinger58335872004-07-09 14:26:18 +0000601 # Wrong type, s/b tuple
602 self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
603 # Wrong length, s/b 625
604 self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None))
605 # Wrong type, s/b tuple of 625 ints
606 self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
607 # Last element s/b an int also
608 self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))
Serhiy Storchaka178f0b62015-07-24 09:02:53 +0300609 # Last element s/b between 0 and 624
610 with self.assertRaises((ValueError, OverflowError)):
611 self.gen.setstate((2, (1,)*624+(625,), None))
612 with self.assertRaises((ValueError, OverflowError)):
613 self.gen.setstate((2, (1,)*624+(-1,), None))
bladebryan9616a822017-04-21 23:10:46 -0700614 # Failed calls to setstate() should not have changed the state.
615 bits100 = self.gen.getrandbits(100)
616 self.gen.setstate(start_state)
617 self.assertEqual(self.gen.getrandbits(100), bits100)
Raymond Hettinger58335872004-07-09 14:26:18 +0000618
R David Murraye3e1c172013-04-02 12:47:23 -0400619 # Little trick to make "tuple(x % (2**32) for x in internalstate)"
620 # raise ValueError. I cannot think of a simple way to achieve this, so
621 # I am opting for using a generator as the middle argument of setstate
622 # which attempts to cast a NaN to integer.
623 state_values = self.gen.getstate()[1]
624 state_values = list(state_values)
625 state_values[-1] = float('nan')
626 state = (int(x) for x in state_values)
627 self.assertRaises(TypeError, self.gen.setstate, (2, state, None))
628
Raymond Hettinger40f62172002-12-29 23:03:38 +0000629 def test_referenceImplementation(self):
630 # Compare the python implementation with results from the original
631 # code. Create 2000 53-bit precision random floats. Compare only
632 # the last ten entries to show that the independent implementations
633 # are tracking. Here is the main() function needed to create the
634 # list of expected random numbers:
635 # void main(void){
636 # int i;
637 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
638 # init_by_array(init, length);
639 # for (i=0; i<2000; i++) {
640 # printf("%.15f ", genrand_res53());
641 # if (i%5==4) printf("\n");
642 # }
643 # }
644 expected = [0.45839803073713259,
645 0.86057815201978782,
646 0.92848331726782152,
647 0.35932681119782461,
648 0.081823493762449573,
649 0.14332226470169329,
650 0.084297823823520024,
651 0.53814864671831453,
652 0.089215024911993401,
653 0.78486196105372907]
654
Guido van Rossume2a383d2007-01-15 16:59:06 +0000655 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000656 actual = self.randomlist(2000)[-10:]
657 for a, e in zip(actual, expected):
658 self.assertAlmostEqual(a,e,places=14)
659
660 def test_strong_reference_implementation(self):
661 # Like test_referenceImplementation, but checks for exact bit-level
662 # equality. This should pass on any box where C double contains
663 # at least 53 bits of precision (the underlying algorithm suffers
664 # no rounding errors -- all results are exact).
665 from math import ldexp
666
Guido van Rossume2a383d2007-01-15 16:59:06 +0000667 expected = [0x0eab3258d2231f,
668 0x1b89db315277a5,
669 0x1db622a5518016,
670 0x0b7f9af0d575bf,
671 0x029e4c4db82240,
672 0x04961892f5d673,
673 0x02b291598e4589,
674 0x11388382c15694,
675 0x02dad977c9e1fe,
676 0x191d96d4d334c6]
677 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000678 actual = self.randomlist(2000)[-10:]
679 for a, e in zip(actual, expected):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000680 self.assertEqual(int(ldexp(a, 53)), e)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000681
682 def test_long_seed(self):
683 # This is most interesting to run in debug mode, just to make sure
684 # nothing blows up. Under the covers, a dynamically resized array
685 # is allocated, consuming space proportional to the number of bits
686 # in the seed. Unfortunately, that's a quadratic-time algorithm,
687 # so don't make this horribly big.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000688 seed = (1 << (10000 * 8)) - 1 # about 10K bytes
Raymond Hettinger40f62172002-12-29 23:03:38 +0000689 self.gen.seed(seed)
690
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000691 def test_53_bits_per_float(self):
692 # This should pass whenever a C double has 53 bit precision.
693 span = 2 ** 53
694 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000695 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000696 cum |= int(self.gen.random() * span)
697 self.assertEqual(cum, span-1)
698
699 def test_bigrand(self):
700 # The randrange routine should build-up the required number of bits
701 # in stages so that all bit positions are active.
702 span = 2 ** 500
703 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000704 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000705 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000706 self.assertTrue(0 <= r < span)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000707 cum |= r
708 self.assertEqual(cum, span-1)
709
710 def test_bigrand_ranges(self):
711 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600712 start = self.gen.randrange(2 ** (i-2))
713 stop = self.gen.randrange(2 ** i)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000714 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600715 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000716 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000717
718 def test_rangelimits(self):
719 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
Raymond Hettingera690a992003-11-16 16:17:49 +0000720 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000721 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000722
Antoine Pitrou75a33782020-04-17 19:32:14 +0200723 def test_getrandbits(self):
724 super().test_getrandbits()
725
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000726 # Verify cross-platform repeatability
727 self.gen.seed(1234567)
728 self.assertEqual(self.gen.getrandbits(100),
Guido van Rossume2a383d2007-01-15 16:59:06 +0000729 97904845777343510404718956115)
Raymond Hettinger58335872004-07-09 14:26:18 +0000730
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200731 def test_randrange_uses_getrandbits(self):
732 # Verify use of getrandbits by randrange
733 # Use same seed as in the cross-platform repeatability test
Antoine Pitrou75a33782020-04-17 19:32:14 +0200734 # in test_getrandbits above.
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200735 self.gen.seed(1234567)
736 # If randrange uses getrandbits, it should pick getrandbits(100)
737 # when called with a 100-bits stop argument.
738 self.assertEqual(self.gen.randrange(2**99),
739 97904845777343510404718956115)
740
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000741 def test_randbelow_logic(self, _log=log, int=int):
742 # check bitcount transition points: 2**i and 2**(i+1)-1
743 # show that: k = int(1.001 + _log(n, 2))
744 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000745 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000746 n = 1 << i # check an exact power of two
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000747 numbits = i+1
748 k = int(1.00001 + _log(n, 2))
749 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000750 self.assertEqual(n, 2**(k-1))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000751
752 n += n - 1 # check 1 below the next power of two
753 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000754 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000755 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000756
757 n -= n >> 15 # check a little farther below the next power of two
758 k = int(1.00001 + _log(n, 2))
759 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000760 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000761
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200762 def test_randbelow_without_getrandbits(self):
R David Murraye3e1c172013-04-02 12:47:23 -0400763 # Random._randbelow() can only use random() when the built-in one
764 # has been overridden but no new getrandbits() method was supplied.
R David Murraye3e1c172013-04-02 12:47:23 -0400765 maxsize = 1<<random.BPF
766 with warnings.catch_warnings():
767 warnings.simplefilter("ignore", UserWarning)
768 # Population range too large (n >= maxsize)
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200769 self.gen._randbelow_without_getrandbits(
770 maxsize+1, maxsize=maxsize
771 )
772 self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize)
Raymond Hettinger4168f1e2020-05-01 10:34:19 -0700773 # issue 33203: test that _randbelow returns zero on
Wolfgang Maier091e95e2018-04-05 17:19:44 +0200774 # n == 0 also in its getrandbits-independent branch.
Raymond Hettinger4168f1e2020-05-01 10:34:19 -0700775 x = self.gen._randbelow_without_getrandbits(0, maxsize=maxsize)
776 self.assertEqual(x, 0)
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200777
R David Murraye3e1c172013-04-02 12:47:23 -0400778 # This might be going too far to test a single line, but because of our
779 # noble aim of achieving 100% test coverage we need to write a case in
780 # which the following line in Random._randbelow() gets executed:
781 #
782 # rem = maxsize % n
783 # limit = (maxsize - rem) / maxsize
784 # r = random()
785 # while r >= limit:
786 # r = random() # <== *This line* <==<
787 #
788 # Therefore, to guarantee that the while loop is executed at least
789 # once, we need to mock random() so that it returns a number greater
790 # than 'limit' the first time it gets called.
791
792 n = 42
793 epsilon = 0.01
794 limit = (maxsize - (maxsize % n)) / maxsize
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200795 with unittest.mock.patch.object(random.Random, 'random') as random_mock:
796 random_mock.side_effect = [limit + epsilon, limit - epsilon]
797 self.gen._randbelow_without_getrandbits(n, maxsize=maxsize)
798 self.assertEqual(random_mock.call_count, 2)
R David Murraye3e1c172013-04-02 12:47:23 -0400799
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000800 def test_randrange_bug_1590891(self):
801 start = 1000000000000
802 stop = -100000000000000000000
803 step = -200
804 x = self.gen.randrange(start, stop, step)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000805 self.assertTrue(stop < x <= start)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000806 self.assertEqual((x+stop)%step, 0)
807
Raymond Hettinger30d00e52016-10-29 16:55:36 -0700808 def test_choices_algorithms(self):
Raymond Hettinger24e42392016-11-13 00:42:56 -0500809 # The various ways of specifying weights should produce the same results
Raymond Hettinger30d00e52016-10-29 16:55:36 -0700810 choices = self.gen.choices
Raymond Hettinger6023d332016-11-21 15:32:08 -0800811 n = 104729
Raymond Hettinger30d00e52016-10-29 16:55:36 -0700812
813 self.gen.seed(8675309)
814 a = self.gen.choices(range(n), k=10000)
815
816 self.gen.seed(8675309)
817 b = self.gen.choices(range(n), [1]*n, k=10000)
818 self.assertEqual(a, b)
819
820 self.gen.seed(8675309)
821 c = self.gen.choices(range(n), cum_weights=range(1, n+1), k=10000)
822 self.assertEqual(a, c)
823
penguindustin96466302019-05-06 14:57:17 -0400824 # American Roulette
Raymond Hettinger77d574d2016-10-29 17:42:36 -0700825 population = ['Red', 'Black', 'Green']
826 weights = [18, 18, 2]
827 cum_weights = [18, 36, 38]
828 expanded_population = ['Red'] * 18 + ['Black'] * 18 + ['Green'] * 2
829
830 self.gen.seed(9035768)
831 a = self.gen.choices(expanded_population, k=10000)
832
833 self.gen.seed(9035768)
834 b = self.gen.choices(population, weights, k=10000)
835 self.assertEqual(a, b)
836
837 self.gen.seed(9035768)
838 c = self.gen.choices(population, cum_weights=cum_weights, k=10000)
839 self.assertEqual(a, c)
840
Victor Stinner9f5fe792020-04-17 19:05:35 +0200841 def test_randbytes(self):
842 super().test_randbytes()
843
844 # Mersenne Twister randbytes() is deterministic
845 # and does not depend on the endian and bitness.
846 seed = 8675309
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300847 expected = b'3\xa8\xf9f\xf4\xa4\xd06\x19\x8f\x9f\x82\x02oe\xf0'
Victor Stinner9f5fe792020-04-17 19:05:35 +0200848
849 self.gen.seed(seed)
850 self.assertEqual(self.gen.randbytes(16), expected)
851
852 # randbytes(0) must not consume any entropy
853 self.gen.seed(seed)
854 self.assertEqual(self.gen.randbytes(0), b'')
855 self.assertEqual(self.gen.randbytes(16), expected)
856
857 # Four randbytes(4) calls give the same output than randbytes(16)
858 self.gen.seed(seed)
859 self.assertEqual(b''.join([self.gen.randbytes(4) for _ in range(4)]),
860 expected)
861
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300862 # Each randbytes(1), randbytes(2) or randbytes(3) call consumes
863 # 4 bytes of entropy
Victor Stinner9f5fe792020-04-17 19:05:35 +0200864 self.gen.seed(seed)
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300865 expected1 = expected[3::4]
866 self.assertEqual(b''.join(self.gen.randbytes(1) for _ in range(4)),
867 expected1)
868
869 self.gen.seed(seed)
870 expected2 = b''.join(expected[i + 2: i + 4]
Victor Stinner9f5fe792020-04-17 19:05:35 +0200871 for i in range(0, len(expected), 4))
872 self.assertEqual(b''.join(self.gen.randbytes(2) for _ in range(4)),
873 expected2)
874
875 self.gen.seed(seed)
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300876 expected3 = b''.join(expected[i + 1: i + 4]
Victor Stinner9f5fe792020-04-17 19:05:35 +0200877 for i in range(0, len(expected), 4))
878 self.assertEqual(b''.join(self.gen.randbytes(3) for _ in range(4)),
879 expected3)
880
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300881 def test_randbytes_getrandbits(self):
882 # There is a simple relation between randbytes() and getrandbits()
883 seed = 2849427419
884 gen2 = random.Random()
885 self.gen.seed(seed)
886 gen2.seed(seed)
887 for n in range(9):
888 self.assertEqual(self.gen.randbytes(n),
889 gen2.getrandbits(n * 8).to_bytes(n, 'little'))
890
Victor Stinner9f5fe792020-04-17 19:05:35 +0200891
Raymond Hettinger2d0c2562009-02-19 09:53:18 +0000892def gamma(z, sqrt2pi=(2.0*pi)**0.5):
893 # Reflection to right half of complex plane
894 if z < 0.5:
895 return pi / sin(pi*z) / gamma(1.0-z)
896 # Lanczos approximation with g=7
897 az = z + (7.0 - 0.5)
898 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([
899 0.9999999999995183,
900 676.5203681218835 / z,
901 -1259.139216722289 / (z+1.0),
902 771.3234287757674 / (z+2.0),
903 -176.6150291498386 / (z+3.0),
904 12.50734324009056 / (z+4.0),
905 -0.1385710331296526 / (z+5.0),
906 0.9934937113930748e-05 / (z+6.0),
907 0.1659470187408462e-06 / (z+7.0),
908 ])
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000909
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000910class TestDistributions(unittest.TestCase):
911 def test_zeroinputs(self):
912 # Verify that distributions can handle a series of zero inputs'
913 g = random.Random()
Guido van Rossum805365e2007-05-07 22:24:25 +0000914 x = [g.random() for i in range(50)] + [0.0]*5
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000915 g.random = x[:].pop; g.uniform(1,10)
916 g.random = x[:].pop; g.paretovariate(1.0)
917 g.random = x[:].pop; g.expovariate(1.0)
918 g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200919 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000920 g.random = x[:].pop; g.normalvariate(0.0, 1.0)
921 g.random = x[:].pop; g.gauss(0.0, 1.0)
922 g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
923 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
924 g.random = x[:].pop; g.gammavariate(0.01, 1.0)
925 g.random = x[:].pop; g.gammavariate(1.0, 1.0)
926 g.random = x[:].pop; g.gammavariate(200.0, 1.0)
927 g.random = x[:].pop; g.betavariate(3.0, 3.0)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000928 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000929
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000930 def test_avg_std(self):
931 # Use integration to test distribution average and standard deviation.
932 # Only works for distributions which do not consume variates in pairs
933 g = random.Random()
934 N = 5000
Guido van Rossum805365e2007-05-07 22:24:25 +0000935 x = [i/float(N) for i in range(1,N)]
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000936 for variate, args, mu, sigmasqrd in [
937 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
Christian Heimesfe337bf2008-03-23 21:54:12 +0000938 (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 +0000939 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200940 (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000941 (g.paretovariate, (5.0,), 5.0/(5.0-1),
942 5.0/((5.0-1)**2*(5.0-2))),
943 (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
944 gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]:
945 g.random = x[:].pop
946 y = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000947 for i in range(len(x)):
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000948 try:
949 y.append(variate(*args))
950 except IndexError:
951 pass
952 s1 = s2 = 0
953 for e in y:
954 s1 += e
955 s2 += (e - mu) ** 2
956 N = len(y)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200957 self.assertAlmostEqual(s1/N, mu, places=2,
958 msg='%s%r' % (variate.__name__, args))
959 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
960 msg='%s%r' % (variate.__name__, args))
961
962 def test_constant(self):
963 g = random.Random()
964 N = 100
965 for variate, args, expected in [
966 (g.uniform, (10.0, 10.0), 10.0),
967 (g.triangular, (10.0, 10.0), 10.0),
Raymond Hettinger978c6ab2014-05-25 17:25:27 -0700968 (g.triangular, (10.0, 10.0, 10.0), 10.0),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200969 (g.expovariate, (float('inf'),), 0.0),
970 (g.vonmisesvariate, (3.0, float('inf')), 3.0),
971 (g.gauss, (10.0, 0.0), 10.0),
972 (g.lognormvariate, (0.0, 0.0), 1.0),
973 (g.lognormvariate, (-float('inf'), 0.0), 0.0),
974 (g.normalvariate, (10.0, 0.0), 10.0),
975 (g.paretovariate, (float('inf'),), 1.0),
976 (g.weibullvariate, (10.0, float('inf')), 10.0),
977 (g.weibullvariate, (0.0, 10.0), 0.0),
978 ]:
979 for i in range(N):
980 self.assertEqual(variate(*args), expected)
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000981
Mark Dickinsonbe5f9192013-02-10 14:16:10 +0000982 def test_von_mises_range(self):
983 # Issue 17149: von mises variates were not consistently in the
984 # range [0, 2*PI].
985 g = random.Random()
986 N = 100
987 for mu in 0.0, 0.1, 3.1, 6.2:
988 for kappa in 0.0, 2.3, 500.0:
989 for _ in range(N):
990 sample = g.vonmisesvariate(mu, kappa)
991 self.assertTrue(
992 0 <= sample <= random.TWOPI,
993 msg=("vonmisesvariate({}, {}) produced a result {} out"
994 " of range [0, 2*pi]").format(mu, kappa, sample))
995
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200996 def test_von_mises_large_kappa(self):
997 # Issue #17141: vonmisesvariate() was hang for large kappas
998 random.vonmisesvariate(0, 1e15)
999 random.vonmisesvariate(0, 1e100)
1000
R David Murraye3e1c172013-04-02 12:47:23 -04001001 def test_gammavariate_errors(self):
1002 # Both alpha and beta must be > 0.0
1003 self.assertRaises(ValueError, random.gammavariate, -1, 3)
1004 self.assertRaises(ValueError, random.gammavariate, 0, 2)
1005 self.assertRaises(ValueError, random.gammavariate, 2, 0)
1006 self.assertRaises(ValueError, random.gammavariate, 1, -3)
1007
leodema63d15222018-12-24 07:54:25 +01001008 # There are three different possibilities in the current implementation
1009 # of random.gammavariate(), depending on the value of 'alpha'. What we
1010 # are going to do here is to fix the values returned by random() to
1011 # generate test cases that provide 100% line coverage of the method.
R David Murraye3e1c172013-04-02 12:47:23 -04001012 @unittest.mock.patch('random.Random.random')
leodema63d15222018-12-24 07:54:25 +01001013 def test_gammavariate_alpha_greater_one(self, random_mock):
R David Murraye3e1c172013-04-02 12:47:23 -04001014
leodema63d15222018-12-24 07:54:25 +01001015 # #1: alpha > 1.0.
1016 # We want the first random number to be outside the
R David Murraye3e1c172013-04-02 12:47:23 -04001017 # [1e-7, .9999999] range, so that the continue statement executes
1018 # once. The values of u1 and u2 will be 0.5 and 0.3, respectively.
1019 random_mock.side_effect = [1e-8, 0.5, 0.3]
1020 returned_value = random.gammavariate(1.1, 2.3)
1021 self.assertAlmostEqual(returned_value, 2.53)
1022
leodema63d15222018-12-24 07:54:25 +01001023 @unittest.mock.patch('random.Random.random')
1024 def test_gammavariate_alpha_equal_one(self, random_mock):
R David Murraye3e1c172013-04-02 12:47:23 -04001025
leodema63d15222018-12-24 07:54:25 +01001026 # #2.a: alpha == 1.
1027 # The execution body of the while loop executes once.
1028 # Then random.random() returns 0.45,
1029 # which causes while to stop looping and the algorithm to terminate.
1030 random_mock.side_effect = [0.45]
1031 returned_value = random.gammavariate(1.0, 3.14)
1032 self.assertAlmostEqual(returned_value, 1.877208182372648)
1033
1034 @unittest.mock.patch('random.Random.random')
1035 def test_gammavariate_alpha_equal_one_equals_expovariate(self, random_mock):
1036
1037 # #2.b: alpha == 1.
1038 # It must be equivalent of calling expovariate(1.0 / beta).
1039 beta = 3.14
1040 random_mock.side_effect = [1e-8, 1e-8]
1041 gammavariate_returned_value = random.gammavariate(1.0, beta)
1042 expovariate_returned_value = random.expovariate(1.0 / beta)
1043 self.assertAlmostEqual(gammavariate_returned_value, expovariate_returned_value)
1044
1045 @unittest.mock.patch('random.Random.random')
1046 def test_gammavariate_alpha_between_zero_and_one(self, random_mock):
1047
1048 # #3: 0 < alpha < 1.
1049 # This is the most complex region of code to cover,
R David Murraye3e1c172013-04-02 12:47:23 -04001050 # as there are multiple if-else statements. Let's take a look at the
1051 # source code, and determine the values that we need accordingly:
1052 #
1053 # while 1:
1054 # u = random()
1055 # b = (_e + alpha)/_e
1056 # p = b*u
1057 # if p <= 1.0: # <=== (A)
1058 # x = p ** (1.0/alpha)
1059 # else: # <=== (B)
1060 # x = -_log((b-p)/alpha)
1061 # u1 = random()
1062 # if p > 1.0: # <=== (C)
1063 # if u1 <= x ** (alpha - 1.0): # <=== (D)
1064 # break
1065 # elif u1 <= _exp(-x): # <=== (E)
1066 # break
1067 # return x * beta
1068 #
1069 # First, we want (A) to be True. For that we need that:
1070 # b*random() <= 1.0
1071 # r1 = random() <= 1.0 / b
1072 #
1073 # We now get to the second if-else branch, and here, since p <= 1.0,
1074 # (C) is False and we take the elif branch, (E). For it to be True,
1075 # so that the break is executed, we need that:
1076 # r2 = random() <= _exp(-x)
1077 # r2 <= _exp(-(p ** (1.0/alpha)))
1078 # r2 <= _exp(-((b*r1) ** (1.0/alpha)))
1079
1080 _e = random._e
1081 _exp = random._exp
1082 _log = random._log
1083 alpha = 0.35
1084 beta = 1.45
1085 b = (_e + alpha)/_e
1086 epsilon = 0.01
1087
1088 r1 = 0.8859296441566 # 1.0 / b
1089 r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha)))
1090
1091 # These four "random" values result in the following trace:
1092 # (A) True, (E) False --> [next iteration of while]
1093 # (A) True, (E) True --> [while loop breaks]
1094 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
1095 returned_value = random.gammavariate(alpha, beta)
1096 self.assertAlmostEqual(returned_value, 1.4499999999997544)
1097
1098 # Let's now make (A) be False. If this is the case, when we get to the
1099 # second if-else 'p' is greater than 1, so (C) evaluates to True. We
1100 # now encounter a second if statement, (D), which in order to execute
1101 # must satisfy the following condition:
1102 # r2 <= x ** (alpha - 1.0)
1103 # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0)
1104 # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0)
1105 r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False
1106 r2 = 0.9445400408898141
1107
1108 # And these four values result in the following trace:
1109 # (B) and (C) True, (D) False --> [next iteration of while]
1110 # (B) and (C) True, (D) True [while loop breaks]
1111 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
1112 returned_value = random.gammavariate(alpha, beta)
1113 self.assertAlmostEqual(returned_value, 1.5830349561760781)
1114
1115 @unittest.mock.patch('random.Random.gammavariate')
1116 def test_betavariate_return_zero(self, gammavariate_mock):
1117 # betavariate() returns zero when the Gamma distribution
1118 # that it uses internally returns this same value.
1119 gammavariate_mock.return_value = 0.0
1120 self.assertEqual(0.0, random.betavariate(2.71828, 3.14159))
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +02001121
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001122
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001123class TestRandomSubclassing(unittest.TestCase):
1124 def test_random_subclass_with_kwargs(self):
1125 # SF bug #1486663 -- this used to erroneously raise a TypeError
1126 class Subclass(random.Random):
1127 def __init__(self, newarg=None):
1128 random.Random.__init__(self)
1129 Subclass(newarg=1)
1130
1131 def test_subclasses_overriding_methods(self):
1132 # Subclasses with an overridden random, but only the original
1133 # getrandbits method should not rely on getrandbits in for randrange,
1134 # but should use a getrandbits-independent implementation instead.
1135
1136 # subclass providing its own random **and** getrandbits methods
1137 # like random.SystemRandom does => keep relying on getrandbits for
1138 # randrange
1139 class SubClass1(random.Random):
1140 def random(self):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001141 called.add('SubClass1.random')
1142 return random.Random.random(self)
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001143
1144 def getrandbits(self, n):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001145 called.add('SubClass1.getrandbits')
1146 return random.Random.getrandbits(self, n)
1147 called = set()
1148 SubClass1().randrange(42)
1149 self.assertEqual(called, {'SubClass1.getrandbits'})
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001150
1151 # subclass providing only random => can only use random for randrange
1152 class SubClass2(random.Random):
1153 def random(self):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001154 called.add('SubClass2.random')
1155 return random.Random.random(self)
1156 called = set()
1157 SubClass2().randrange(42)
1158 self.assertEqual(called, {'SubClass2.random'})
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001159
1160 # subclass defining getrandbits to complement its inherited random
1161 # => can now rely on getrandbits for randrange again
1162 class SubClass3(SubClass2):
1163 def getrandbits(self, n):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001164 called.add('SubClass3.getrandbits')
1165 return random.Random.getrandbits(self, n)
1166 called = set()
1167 SubClass3().randrange(42)
1168 self.assertEqual(called, {'SubClass3.getrandbits'})
1169
1170 # subclass providing only random and inherited getrandbits
1171 # => random takes precedence
1172 class SubClass4(SubClass3):
1173 def random(self):
1174 called.add('SubClass4.random')
1175 return random.Random.random(self)
1176 called = set()
1177 SubClass4().randrange(42)
1178 self.assertEqual(called, {'SubClass4.random'})
1179
1180 # Following subclasses don't define random or getrandbits directly,
1181 # but inherit them from classes which are not subclasses of Random
1182 class Mixin1:
1183 def random(self):
1184 called.add('Mixin1.random')
1185 return random.Random.random(self)
1186 class Mixin2:
1187 def getrandbits(self, n):
1188 called.add('Mixin2.getrandbits')
1189 return random.Random.getrandbits(self, n)
1190
1191 class SubClass5(Mixin1, random.Random):
1192 pass
1193 called = set()
1194 SubClass5().randrange(42)
1195 self.assertEqual(called, {'Mixin1.random'})
1196
1197 class SubClass6(Mixin2, random.Random):
1198 pass
1199 called = set()
1200 SubClass6().randrange(42)
1201 self.assertEqual(called, {'Mixin2.getrandbits'})
1202
1203 class SubClass7(Mixin1, Mixin2, random.Random):
1204 pass
1205 called = set()
1206 SubClass7().randrange(42)
1207 self.assertEqual(called, {'Mixin1.random'})
1208
1209 class SubClass8(Mixin2, Mixin1, random.Random):
1210 pass
1211 called = set()
1212 SubClass8().randrange(42)
1213 self.assertEqual(called, {'Mixin2.getrandbits'})
1214
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001215
Raymond Hettinger40f62172002-12-29 23:03:38 +00001216class TestModule(unittest.TestCase):
1217 def testMagicConstants(self):
1218 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
1219 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
1220 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
1221 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
1222
1223 def test__all__(self):
1224 # tests validity but not completeness of the __all__ list
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001225 self.assertTrue(set(random.__all__) <= set(dir(random)))
Raymond Hettinger40f62172002-12-29 23:03:38 +00001226
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001227 @unittest.skipUnless(hasattr(os, "fork"), "fork() required")
1228 def test_after_fork(self):
1229 # Test the global Random instance gets reseeded in child
1230 r, w = os.pipe()
Victor Stinnerda5e9302017-08-09 17:59:05 +02001231 pid = os.fork()
1232 if pid == 0:
1233 # child process
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001234 try:
1235 val = random.getrandbits(128)
1236 with open(w, "w") as f:
1237 f.write(str(val))
1238 finally:
1239 os._exit(0)
1240 else:
Victor Stinnerda5e9302017-08-09 17:59:05 +02001241 # parent process
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001242 os.close(w)
1243 val = random.getrandbits(128)
1244 with open(r, "r") as f:
1245 child_val = eval(f.read())
1246 self.assertNotEqual(val, child_val)
1247
Victor Stinner278c1e12020-03-31 20:08:12 +02001248 support.wait_process(pid, exitcode=0)
Victor Stinnerda5e9302017-08-09 17:59:05 +02001249
Thomas Woutersb2137042007-02-01 18:02:27 +00001250
Raymond Hettinger40f62172002-12-29 23:03:38 +00001251if __name__ == "__main__":
Ezio Melotti3e4a98b2013-04-19 05:45:27 +03001252 unittest.main()