blob: 35ae4e6c3c80cee008687b3003e74db2e6fc89f9 [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 Na6989af02020-06-21 18:44:58 +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
masklinn1e27b572020-12-19 05:33:36 +010014from collections import abc, 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
masklinn1e27b572020-12-19 05:33:36 +0100166 def test_sample_on_seqsets(self):
167 class SeqSet(abc.Sequence, abc.Set):
168 def __init__(self, items):
169 self._items = items
170
171 def __len__(self):
172 return len(self._items)
173
174 def __getitem__(self, index):
175 return self._items[index]
176
177 population = SeqSet([2, 4, 1, 3])
178 with warnings.catch_warnings():
179 warnings.simplefilter("error", DeprecationWarning)
180 self.gen.sample(population, k=2)
181
Raymond Hettinger81a5fc32020-05-08 07:53:15 -0700182 def test_sample_with_counts(self):
183 sample = self.gen.sample
184
185 # General case
186 colors = ['red', 'green', 'blue', 'orange', 'black', 'brown', 'amber']
187 counts = [500, 200, 20, 10, 5, 0, 1 ]
188 k = 700
189 summary = Counter(sample(colors, counts=counts, k=k))
190 self.assertEqual(sum(summary.values()), k)
191 for color, weight in zip(colors, counts):
192 self.assertLessEqual(summary[color], weight)
193 self.assertNotIn('brown', summary)
194
195 # Case that exhausts the population
196 k = sum(counts)
197 summary = Counter(sample(colors, counts=counts, k=k))
198 self.assertEqual(sum(summary.values()), k)
199 for color, weight in zip(colors, counts):
200 self.assertLessEqual(summary[color], weight)
201 self.assertNotIn('brown', summary)
202
203 # Case with population size of 1
204 summary = Counter(sample(['x'], counts=[10], k=8))
205 self.assertEqual(summary, Counter(x=8))
206
207 # Case with all counts equal.
208 nc = len(colors)
209 summary = Counter(sample(colors, counts=[10]*nc, k=10*nc))
210 self.assertEqual(summary, Counter(10*colors))
211
212 # Test error handling
213 with self.assertRaises(TypeError):
214 sample(['red', 'green', 'blue'], counts=10, k=10) # counts not iterable
215 with self.assertRaises(ValueError):
216 sample(['red', 'green', 'blue'], counts=[-3, -7, -8], k=2) # counts are negative
217 with self.assertRaises(ValueError):
218 sample(['red', 'green', 'blue'], counts=[0, 0, 0], k=2) # counts are zero
219 with self.assertRaises(ValueError):
220 sample(['red', 'green'], counts=[10, 10], k=21) # population too small
221 with self.assertRaises(ValueError):
222 sample(['red', 'green', 'blue'], counts=[1, 2], k=2) # too few counts
223 with self.assertRaises(ValueError):
224 sample(['red', 'green', 'blue'], counts=[1, 2, 3, 4], k=2) # too many counts
225
Raymond Hettinger28aa4a02016-09-07 00:08:44 -0700226 def test_choices(self):
227 choices = self.gen.choices
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700228 data = ['red', 'green', 'blue', 'yellow']
229 str_data = 'abcd'
230 range_data = range(4)
231 set_data = set(range(4))
232
233 # basic functionality
234 for sample in [
Raymond Hettinger9016f282016-09-26 21:45:57 -0700235 choices(data, k=5),
236 choices(data, range(4), k=5),
Raymond Hettinger28aa4a02016-09-07 00:08:44 -0700237 choices(k=5, population=data, weights=range(4)),
238 choices(k=5, population=data, cum_weights=range(4)),
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700239 ]:
240 self.assertEqual(len(sample), 5)
241 self.assertEqual(type(sample), list)
242 self.assertTrue(set(sample) <= set(data))
243
244 # test argument handling
Raymond Hettinger28aa4a02016-09-07 00:08:44 -0700245 with self.assertRaises(TypeError): # missing arguments
246 choices(2)
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700247
Raymond Hettinger9016f282016-09-26 21:45:57 -0700248 self.assertEqual(choices(data, k=0), []) # k == 0
249 self.assertEqual(choices(data, k=-1), []) # negative k behaves like ``[0] * -1``
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700250 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700251 choices(data, k=2.5) # k is a float
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700252
Raymond Hettinger9016f282016-09-26 21:45:57 -0700253 self.assertTrue(set(choices(str_data, k=5)) <= set(str_data)) # population is a string sequence
254 self.assertTrue(set(choices(range_data, k=5)) <= set(range_data)) # population is a range
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700255 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700256 choices(set_data, k=2) # population is not a sequence
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700257
Raymond Hettinger9016f282016-09-26 21:45:57 -0700258 self.assertTrue(set(choices(data, None, k=5)) <= set(data)) # weights is None
259 self.assertTrue(set(choices(data, weights=None, k=5)) <= set(data))
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700260 with self.assertRaises(ValueError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700261 choices(data, [1,2], k=5) # len(weights) != len(population)
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700262 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700263 choices(data, 10, k=5) # non-iterable weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700264 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700265 choices(data, [None]*4, k=5) # non-numeric weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700266 for weights in [
267 [15, 10, 25, 30], # integer weights
268 [15.1, 10.2, 25.2, 30.3], # float weights
269 [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights
270 [True, False, True, False] # booleans (include / exclude)
271 ]:
Raymond Hettinger9016f282016-09-26 21:45:57 -0700272 self.assertTrue(set(choices(data, weights, k=5)) <= set(data))
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700273
274 with self.assertRaises(ValueError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700275 choices(data, cum_weights=[1,2], k=5) # len(weights) != len(population)
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700276 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700277 choices(data, cum_weights=10, k=5) # non-iterable cum_weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700278 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700279 choices(data, cum_weights=[None]*4, k=5) # non-numeric cum_weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700280 with self.assertRaises(TypeError):
Raymond Hettinger9016f282016-09-26 21:45:57 -0700281 choices(data, range(4), cum_weights=range(4), k=5) # both weights and cum_weights
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700282 for weights in [
283 [15, 10, 25, 30], # integer cum_weights
284 [15.1, 10.2, 25.2, 30.3], # float cum_weights
285 [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional cum_weights
286 ]:
Raymond Hettinger9016f282016-09-26 21:45:57 -0700287 self.assertTrue(set(choices(data, cum_weights=weights, k=5)) <= set(data))
Raymond Hettingere8f1e002016-09-06 17:15:29 -0700288
Raymond Hettinger7b166522016-10-14 01:19:38 -0400289 # Test weight focused on a single element of the population
290 self.assertEqual(choices('abcd', [1, 0, 0, 0]), ['a'])
291 self.assertEqual(choices('abcd', [0, 1, 0, 0]), ['b'])
292 self.assertEqual(choices('abcd', [0, 0, 1, 0]), ['c'])
293 self.assertEqual(choices('abcd', [0, 0, 0, 1]), ['d'])
294
295 # Test consistency with random.choice() for empty population
296 with self.assertRaises(IndexError):
297 choices([], k=1)
298 with self.assertRaises(IndexError):
299 choices([], weights=[], k=1)
300 with self.assertRaises(IndexError):
301 choices([], cum_weights=[], k=5)
302
Raymond Hettingerddf71712018-06-27 01:08:31 -0700303 def test_choices_subnormal(self):
Min ho Kim96e12d52019-07-22 06:12:33 +1000304 # Subnormal weights would occasionally trigger an IndexError
Raymond Hettingerddf71712018-06-27 01:08:31 -0700305 # in choices() when the value returned by random() was large
306 # enough to make `random() * total` round up to the total.
307 # See https://bugs.python.org/msg275594 for more detail.
308 choices = self.gen.choices
309 choices(population=[1, 2], weights=[1e-323, 1e-323], k=5000)
310
Raymond Hettinger041d8b42019-11-23 02:22:13 -0800311 def test_choices_with_all_zero_weights(self):
312 # See issue #38881
313 with self.assertRaises(ValueError):
314 self.gen.choices('AB', [0.0, 0.0])
315
Ram Rachumb0dfc752020-09-29 04:32:10 +0300316 def test_choices_negative_total(self):
317 with self.assertRaises(ValueError):
318 self.gen.choices('ABC', [3, -5, 1])
319
320 def test_choices_infinite_total(self):
321 with self.assertRaises(ValueError):
322 self.gen.choices('A', [float('inf')])
323 with self.assertRaises(ValueError):
324 self.gen.choices('AB', [0.0, float('inf')])
325 with self.assertRaises(ValueError):
326 self.gen.choices('AB', [-float('inf'), 123])
327 with self.assertRaises(ValueError):
328 self.gen.choices('AB', [0.0, float('nan')])
329 with self.assertRaises(ValueError):
330 self.gen.choices('AB', [float('-inf'), float('inf')])
331
Raymond Hettinger40f62172002-12-29 23:03:38 +0000332 def test_gauss(self):
333 # Ensure that the seed() method initializes all the hidden state. In
334 # particular, through 2.2.1 it failed to reset a piece of state used
335 # by (and only by) the .gauss() method.
336
337 for seed in 1, 12, 123, 1234, 12345, 123456, 654321:
338 self.gen.seed(seed)
339 x1 = self.gen.random()
340 y1 = self.gen.gauss(0, 1)
341
342 self.gen.seed(seed)
343 x2 = self.gen.random()
344 y2 = self.gen.gauss(0, 1)
345
346 self.assertEqual(x1, x2)
347 self.assertEqual(y1, y2)
348
Antoine Pitrou75a33782020-04-17 19:32:14 +0200349 def test_getrandbits(self):
350 # Verify ranges
351 for k in range(1, 1000):
352 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k)
353 self.assertEqual(self.gen.getrandbits(0), 0)
354
355 # Verify all bits active
356 getbits = self.gen.getrandbits
357 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]:
358 all_bits = 2**span-1
359 cum = 0
360 cpl_cum = 0
361 for i in range(100):
362 v = getbits(span)
363 cum |= v
364 cpl_cum |= all_bits ^ v
365 self.assertEqual(cum, all_bits)
366 self.assertEqual(cpl_cum, all_bits)
367
368 # Verify argument checking
369 self.assertRaises(TypeError, self.gen.getrandbits)
370 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2)
371 self.assertRaises(ValueError, self.gen.getrandbits, -1)
372 self.assertRaises(TypeError, self.gen.getrandbits, 10.1)
373
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000374 def test_pickling(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +0200375 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
376 state = pickle.dumps(self.gen, proto)
377 origseq = [self.gen.random() for i in range(10)]
378 newgen = pickle.loads(state)
379 restoredseq = [newgen.random() for i in range(10)]
380 self.assertEqual(origseq, restoredseq)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000381
Dong-hee Na6989af02020-06-21 18:44:58 +0900382 @test.support.cpython_only
383 def test_bug_41052(self):
384 # _random.Random should not be allowed to serialization
385 import _random
386 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
387 r = _random.Random()
388 self.assertRaises(TypeError, pickle.dumps, r, proto)
389
AMIRb8fde8b2020-12-22 03:15:50 +0330390 @test.support.cpython_only
391 def test_bug_42008(self):
392 # _random.Random should call seed with first element of arg tuple
393 import _random
394 r1 = _random.Random()
395 r1.seed(8675309)
396 r2 = _random.Random(8675309)
397 self.assertEqual(r1.random(), r2.random())
398
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000399 def test_bug_1727780(self):
400 # verify that version-2-pickles can be loaded
401 # fine, whether they are created on 32-bit or 64-bit
402 # platforms, and that version-3-pickles load fine.
403 files = [("randv2_32.pck", 780),
404 ("randv2_64.pck", 866),
405 ("randv3.pck", 343)]
406 for file, value in files:
Serhiy Storchaka5b10b982019-03-05 10:06:26 +0200407 with open(support.findfile(file),"rb") as f:
408 r = pickle.load(f)
Raymond Hettinger05156612010-09-07 04:44:52 +0000409 self.assertEqual(int(r.random()*1000), value)
410
411 def test_bug_9025(self):
412 # Had problem with an uneven distribution in int(n*random())
413 # Verify the fix by checking that distributions fall within expectations.
414 n = 100000
415 randrange = self.gen.randrange
416 k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
417 self.assertTrue(0.30 < k/n < .37, (k/n))
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000418
Victor Stinner9f5fe792020-04-17 19:05:35 +0200419 def test_randbytes(self):
420 # Verify ranges
421 for n in range(1, 10):
422 data = self.gen.randbytes(n)
423 self.assertEqual(type(data), bytes)
424 self.assertEqual(len(data), n)
425
426 self.assertEqual(self.gen.randbytes(0), b'')
427
428 # Verify argument checking
429 self.assertRaises(TypeError, self.gen.randbytes)
430 self.assertRaises(TypeError, self.gen.randbytes, 1, 2)
431 self.assertRaises(ValueError, self.gen.randbytes, -1)
432 self.assertRaises(TypeError, self.gen.randbytes, 1.0)
433
434
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300435try:
436 random.SystemRandom().random()
437except NotImplementedError:
438 SystemRandom_available = False
439else:
440 SystemRandom_available = True
441
442@unittest.skipUnless(SystemRandom_available, "random.SystemRandom not available")
443class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
Raymond Hettinger23f12412004-09-13 22:23:21 +0000444 gen = random.SystemRandom()
Raymond Hettinger356a4592004-08-30 06:14:31 +0000445
446 def test_autoseed(self):
447 # Doesn't need to do anything except not fail
448 self.gen.seed()
449
450 def test_saverestore(self):
451 self.assertRaises(NotImplementedError, self.gen.getstate)
452 self.assertRaises(NotImplementedError, self.gen.setstate, None)
453
454 def test_seedargs(self):
455 # Doesn't need to do anything except not fail
456 self.gen.seed(100)
457
Raymond Hettinger356a4592004-08-30 06:14:31 +0000458 def test_gauss(self):
459 self.gen.gauss_next = None
460 self.gen.seed(100)
461 self.assertEqual(self.gen.gauss_next, None)
462
463 def test_pickling(self):
Serhiy Storchakabad12572014-12-15 14:03:42 +0200464 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
465 self.assertRaises(NotImplementedError, pickle.dumps, self.gen, proto)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000466
467 def test_53_bits_per_float(self):
468 # This should pass whenever a C double has 53 bit precision.
469 span = 2 ** 53
470 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000471 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000472 cum |= int(self.gen.random() * span)
473 self.assertEqual(cum, span-1)
474
475 def test_bigrand(self):
476 # The randrange routine should build-up the required number of bits
477 # in stages so that all bit positions are active.
478 span = 2 ** 500
479 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000480 for i in range(100):
Raymond Hettinger356a4592004-08-30 06:14:31 +0000481 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000482 self.assertTrue(0 <= r < span)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000483 cum |= r
484 self.assertEqual(cum, span-1)
485
486 def test_bigrand_ranges(self):
487 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600488 start = self.gen.randrange(2 ** (i-2))
489 stop = self.gen.randrange(2 ** i)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000490 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600491 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000492 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger356a4592004-08-30 06:14:31 +0000493
494 def test_rangelimits(self):
495 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
496 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000497 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000498
R David Murraye3e1c172013-04-02 12:47:23 -0400499 def test_randrange_nonunit_step(self):
500 rint = self.gen.randrange(0, 10, 2)
501 self.assertIn(rint, (0, 2, 4, 6, 8))
502 rint = self.gen.randrange(0, 2, 2)
503 self.assertEqual(rint, 0)
504
505 def test_randrange_errors(self):
506 raises = partial(self.assertRaises, ValueError, self.gen.randrange)
507 # Empty range
508 raises(3, 3)
509 raises(-721)
510 raises(0, 100, -12)
511 # Non-integer start/stop
512 raises(3.14159)
513 raises(0, 2.71828)
514 # Zero and non-integer step
515 raises(0, 42, 0)
516 raises(0, 42, 3.14159)
517
Raymond Hettingera9621bb2020-12-28 11:10:34 -0800518 def test_randrange_argument_handling(self):
519 randrange = self.gen.randrange
520 with self.assertWarns(DeprecationWarning):
521 randrange(10.0, 20, 2)
522 with self.assertWarns(DeprecationWarning):
523 randrange(10, 20.0, 2)
524 with self.assertWarns(DeprecationWarning):
525 randrange(10, 20, 1.0)
526 with self.assertWarns(DeprecationWarning):
527 randrange(10, 20, 2.0)
528 with self.assertWarns(DeprecationWarning):
529 with self.assertRaises(ValueError):
530 randrange(10.5)
531 with self.assertWarns(DeprecationWarning):
532 with self.assertRaises(ValueError):
533 randrange(10, 20.5)
534 with self.assertWarns(DeprecationWarning):
535 with self.assertRaises(ValueError):
536 randrange(10, 20, 1.5)
537
Raymond Hettinger768fa142021-01-02 10:24:51 -0800538 def test_randrange_step(self):
539 # bpo-42772: When stop is None, the step argument was being ignored.
540 randrange = self.gen.randrange
541 with self.assertRaises(TypeError):
542 randrange(1000, step=100)
543 with self.assertRaises(TypeError):
544 randrange(1000, None, step=100)
545
Raymond Hettinger356a4592004-08-30 06:14:31 +0000546 def test_randbelow_logic(self, _log=log, int=int):
547 # check bitcount transition points: 2**i and 2**(i+1)-1
548 # show that: k = int(1.001 + _log(n, 2))
549 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000550 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000551 n = 1 << i # check an exact power of two
Raymond Hettinger356a4592004-08-30 06:14:31 +0000552 numbits = i+1
553 k = int(1.00001 + _log(n, 2))
554 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000555 self.assertEqual(n, 2**(k-1))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000556
557 n += n - 1 # check 1 below the next power of two
558 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000559 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000560 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger356a4592004-08-30 06:14:31 +0000561
562 n -= n >> 15 # check a little farther below the next power of two
563 k = int(1.00001 + _log(n, 2))
564 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000565 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger356a4592004-08-30 06:14:31 +0000566
567
Ezio Melotti3e4a98b2013-04-19 05:45:27 +0300568class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
Raymond Hettinger40f62172002-12-29 23:03:38 +0000569 gen = random.Random()
570
Raymond Hettingerf763a722010-09-07 00:38:15 +0000571 def test_guaranteed_stable(self):
572 # These sequences are guaranteed to stay the same across versions of python
573 self.gen.seed(3456147, version=1)
574 self.assertEqual([self.gen.random().hex() for i in range(4)],
575 ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1',
576 '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000577 self.gen.seed("the quick brown fox", version=2)
578 self.assertEqual([self.gen.random().hex() for i in range(4)],
Raymond Hettinger3fcf0022010-12-08 01:13:53 +0000579 ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4',
580 '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1'])
Raymond Hettingerf763a722010-09-07 00:38:15 +0000581
Raymond Hettingerc7bab7c2016-08-31 15:01:08 -0700582 def test_bug_27706(self):
583 # Verify that version 1 seeds are unaffected by hash randomization
584
585 self.gen.seed('nofar', version=1) # hash('nofar') == 5990528763808513177
586 self.assertEqual([self.gen.random().hex() for i in range(4)],
587 ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5',
588 '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6'])
589
590 self.gen.seed('rachel', version=1) # hash('rachel') == -9091735575445484789
591 self.assertEqual([self.gen.random().hex() for i in range(4)],
592 ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3',
593 '0x1.3052b9c072678p-2', '0x1.578f332106574p-3'])
594
595 self.gen.seed('', version=1) # hash('') == 0
596 self.assertEqual([self.gen.random().hex() for i in range(4)],
597 ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1',
598 '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2'])
599
Oren Milmand780b2d2017-09-28 10:50:01 +0300600 def test_bug_31478(self):
601 # There shouldn't be an assertion failure in _random.Random.seed() in
602 # case the argument has a bad __abs__() method.
603 class BadInt(int):
604 def __abs__(self):
605 return None
606 try:
607 self.gen.seed(BadInt())
608 except TypeError:
609 pass
610
Raymond Hettinger132a7d72017-09-17 09:04:30 -0700611 def test_bug_31482(self):
612 # Verify that version 1 seeds are unaffected by hash randomization
613 # when the seeds are expressed as bytes rather than strings.
614 # The hash(b) values listed are the Python2.7 hash() values
615 # which were used for seeding.
616
617 self.gen.seed(b'nofar', version=1) # hash('nofar') == 5990528763808513177
618 self.assertEqual([self.gen.random().hex() for i in range(4)],
619 ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5',
620 '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6'])
621
622 self.gen.seed(b'rachel', version=1) # hash('rachel') == -9091735575445484789
623 self.assertEqual([self.gen.random().hex() for i in range(4)],
624 ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3',
625 '0x1.3052b9c072678p-2', '0x1.578f332106574p-3'])
626
627 self.gen.seed(b'', version=1) # hash('') == 0
628 self.assertEqual([self.gen.random().hex() for i in range(4)],
629 ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1',
630 '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2'])
631
632 b = b'\x00\x20\x40\x60\x80\xA0\xC0\xE0\xF0'
633 self.gen.seed(b, version=1) # hash(b) == 5015594239749365497
634 self.assertEqual([self.gen.random().hex() for i in range(4)],
635 ['0x1.52c2fde444d23p-1', '0x1.875174f0daea4p-2',
636 '0x1.9e9b2c50e5cd2p-1', '0x1.fa57768bd321cp-2'])
637
Raymond Hettinger58335872004-07-09 14:26:18 +0000638 def test_setstate_first_arg(self):
639 self.assertRaises(ValueError, self.gen.setstate, (1, None, None))
640
641 def test_setstate_middle_arg(self):
bladebryan9616a822017-04-21 23:10:46 -0700642 start_state = self.gen.getstate()
Raymond Hettinger58335872004-07-09 14:26:18 +0000643 # Wrong type, s/b tuple
644 self.assertRaises(TypeError, self.gen.setstate, (2, None, None))
645 # Wrong length, s/b 625
646 self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None))
647 # Wrong type, s/b tuple of 625 ints
648 self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None))
649 # Last element s/b an int also
650 self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None))
Serhiy Storchaka178f0b62015-07-24 09:02:53 +0300651 # Last element s/b between 0 and 624
652 with self.assertRaises((ValueError, OverflowError)):
653 self.gen.setstate((2, (1,)*624+(625,), None))
654 with self.assertRaises((ValueError, OverflowError)):
655 self.gen.setstate((2, (1,)*624+(-1,), None))
bladebryan9616a822017-04-21 23:10:46 -0700656 # Failed calls to setstate() should not have changed the state.
657 bits100 = self.gen.getrandbits(100)
658 self.gen.setstate(start_state)
659 self.assertEqual(self.gen.getrandbits(100), bits100)
Raymond Hettinger58335872004-07-09 14:26:18 +0000660
R David Murraye3e1c172013-04-02 12:47:23 -0400661 # Little trick to make "tuple(x % (2**32) for x in internalstate)"
662 # raise ValueError. I cannot think of a simple way to achieve this, so
663 # I am opting for using a generator as the middle argument of setstate
664 # which attempts to cast a NaN to integer.
665 state_values = self.gen.getstate()[1]
666 state_values = list(state_values)
667 state_values[-1] = float('nan')
668 state = (int(x) for x in state_values)
669 self.assertRaises(TypeError, self.gen.setstate, (2, state, None))
670
Raymond Hettinger40f62172002-12-29 23:03:38 +0000671 def test_referenceImplementation(self):
672 # Compare the python implementation with results from the original
673 # code. Create 2000 53-bit precision random floats. Compare only
674 # the last ten entries to show that the independent implementations
675 # are tracking. Here is the main() function needed to create the
676 # list of expected random numbers:
677 # void main(void){
678 # int i;
679 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
680 # init_by_array(init, length);
681 # for (i=0; i<2000; i++) {
682 # printf("%.15f ", genrand_res53());
683 # if (i%5==4) printf("\n");
684 # }
685 # }
686 expected = [0.45839803073713259,
687 0.86057815201978782,
688 0.92848331726782152,
689 0.35932681119782461,
690 0.081823493762449573,
691 0.14332226470169329,
692 0.084297823823520024,
693 0.53814864671831453,
694 0.089215024911993401,
695 0.78486196105372907]
696
Guido van Rossume2a383d2007-01-15 16:59:06 +0000697 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000698 actual = self.randomlist(2000)[-10:]
699 for a, e in zip(actual, expected):
700 self.assertAlmostEqual(a,e,places=14)
701
702 def test_strong_reference_implementation(self):
703 # Like test_referenceImplementation, but checks for exact bit-level
704 # equality. This should pass on any box where C double contains
705 # at least 53 bits of precision (the underlying algorithm suffers
706 # no rounding errors -- all results are exact).
707 from math import ldexp
708
Guido van Rossume2a383d2007-01-15 16:59:06 +0000709 expected = [0x0eab3258d2231f,
710 0x1b89db315277a5,
711 0x1db622a5518016,
712 0x0b7f9af0d575bf,
713 0x029e4c4db82240,
714 0x04961892f5d673,
715 0x02b291598e4589,
716 0x11388382c15694,
717 0x02dad977c9e1fe,
718 0x191d96d4d334c6]
719 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96))
Raymond Hettinger40f62172002-12-29 23:03:38 +0000720 actual = self.randomlist(2000)[-10:]
721 for a, e in zip(actual, expected):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000722 self.assertEqual(int(ldexp(a, 53)), e)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000723
724 def test_long_seed(self):
725 # This is most interesting to run in debug mode, just to make sure
726 # nothing blows up. Under the covers, a dynamically resized array
727 # is allocated, consuming space proportional to the number of bits
728 # in the seed. Unfortunately, that's a quadratic-time algorithm,
729 # so don't make this horribly big.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000730 seed = (1 << (10000 * 8)) - 1 # about 10K bytes
Raymond Hettinger40f62172002-12-29 23:03:38 +0000731 self.gen.seed(seed)
732
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000733 def test_53_bits_per_float(self):
734 # This should pass whenever a C double has 53 bit precision.
735 span = 2 ** 53
736 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000737 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000738 cum |= int(self.gen.random() * span)
739 self.assertEqual(cum, span-1)
740
741 def test_bigrand(self):
742 # The randrange routine should build-up the required number of bits
743 # in stages so that all bit positions are active.
744 span = 2 ** 500
745 cum = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000746 for i in range(100):
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000747 r = self.gen.randrange(span)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000748 self.assertTrue(0 <= r < span)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000749 cum |= r
750 self.assertEqual(cum, span-1)
751
752 def test_bigrand_ranges(self):
753 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]:
Zachary Warea6edea52013-11-26 14:50:10 -0600754 start = self.gen.randrange(2 ** (i-2))
755 stop = self.gen.randrange(2 ** i)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000756 if stop <= start:
Zachary Warea6edea52013-11-26 14:50:10 -0600757 continue
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000758 self.assertTrue(start <= self.gen.randrange(start, stop) < stop)
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000759
760 def test_rangelimits(self):
761 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]:
Raymond Hettingera690a992003-11-16 16:17:49 +0000762 self.assertEqual(set(range(start,stop)),
Guido van Rossum805365e2007-05-07 22:24:25 +0000763 set([self.gen.randrange(start,stop) for i in range(100)]))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000764
Antoine Pitrou75a33782020-04-17 19:32:14 +0200765 def test_getrandbits(self):
766 super().test_getrandbits()
767
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000768 # Verify cross-platform repeatability
769 self.gen.seed(1234567)
770 self.assertEqual(self.gen.getrandbits(100),
Guido van Rossume2a383d2007-01-15 16:59:06 +0000771 97904845777343510404718956115)
Raymond Hettinger58335872004-07-09 14:26:18 +0000772
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200773 def test_randrange_uses_getrandbits(self):
774 # Verify use of getrandbits by randrange
775 # Use same seed as in the cross-platform repeatability test
Antoine Pitrou75a33782020-04-17 19:32:14 +0200776 # in test_getrandbits above.
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200777 self.gen.seed(1234567)
778 # If randrange uses getrandbits, it should pick getrandbits(100)
779 # when called with a 100-bits stop argument.
780 self.assertEqual(self.gen.randrange(2**99),
781 97904845777343510404718956115)
782
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000783 def test_randbelow_logic(self, _log=log, int=int):
784 # check bitcount transition points: 2**i and 2**(i+1)-1
785 # show that: k = int(1.001 + _log(n, 2))
786 # is equal to or one greater than the number of bits in n
Guido van Rossum805365e2007-05-07 22:24:25 +0000787 for i in range(1, 1000):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000788 n = 1 << i # check an exact power of two
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000789 numbits = i+1
790 k = int(1.00001 + _log(n, 2))
791 self.assertEqual(k, numbits)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000792 self.assertEqual(n, 2**(k-1))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000793
794 n += n - 1 # check 1 below the next power of two
795 k = int(1.00001 + _log(n, 2))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000796 self.assertIn(k, [numbits, numbits+1])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000797 self.assertTrue(2**k > n > 2**(k-2))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000798
799 n -= n >> 15 # check a little farther below the next power of two
800 k = int(1.00001 + _log(n, 2))
801 self.assertEqual(k, numbits) # note the stronger assertion
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000802 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000803
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200804 def test_randbelow_without_getrandbits(self):
R David Murraye3e1c172013-04-02 12:47:23 -0400805 # Random._randbelow() can only use random() when the built-in one
806 # has been overridden but no new getrandbits() method was supplied.
R David Murraye3e1c172013-04-02 12:47:23 -0400807 maxsize = 1<<random.BPF
808 with warnings.catch_warnings():
809 warnings.simplefilter("ignore", UserWarning)
810 # Population range too large (n >= maxsize)
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200811 self.gen._randbelow_without_getrandbits(
812 maxsize+1, maxsize=maxsize
813 )
814 self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize)
Raymond Hettinger4168f1e2020-05-01 10:34:19 -0700815 # issue 33203: test that _randbelow returns zero on
Wolfgang Maier091e95e2018-04-05 17:19:44 +0200816 # n == 0 also in its getrandbits-independent branch.
Raymond Hettinger4168f1e2020-05-01 10:34:19 -0700817 x = self.gen._randbelow_without_getrandbits(0, maxsize=maxsize)
818 self.assertEqual(x, 0)
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200819
R David Murraye3e1c172013-04-02 12:47:23 -0400820 # This might be going too far to test a single line, but because of our
821 # noble aim of achieving 100% test coverage we need to write a case in
822 # which the following line in Random._randbelow() gets executed:
823 #
824 # rem = maxsize % n
825 # limit = (maxsize - rem) / maxsize
826 # r = random()
827 # while r >= limit:
828 # r = random() # <== *This line* <==<
829 #
830 # Therefore, to guarantee that the while loop is executed at least
831 # once, we need to mock random() so that it returns a number greater
832 # than 'limit' the first time it gets called.
833
834 n = 42
835 epsilon = 0.01
836 limit = (maxsize - (maxsize % n)) / maxsize
Wolfgang Maierba3a87a2018-04-17 17:16:17 +0200837 with unittest.mock.patch.object(random.Random, 'random') as random_mock:
838 random_mock.side_effect = [limit + epsilon, limit - epsilon]
839 self.gen._randbelow_without_getrandbits(n, maxsize=maxsize)
840 self.assertEqual(random_mock.call_count, 2)
R David Murraye3e1c172013-04-02 12:47:23 -0400841
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000842 def test_randrange_bug_1590891(self):
843 start = 1000000000000
844 stop = -100000000000000000000
845 step = -200
846 x = self.gen.randrange(start, stop, step)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000847 self.assertTrue(stop < x <= start)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000848 self.assertEqual((x+stop)%step, 0)
849
Raymond Hettinger30d00e52016-10-29 16:55:36 -0700850 def test_choices_algorithms(self):
Raymond Hettinger24e42392016-11-13 00:42:56 -0500851 # The various ways of specifying weights should produce the same results
Raymond Hettinger30d00e52016-10-29 16:55:36 -0700852 choices = self.gen.choices
Raymond Hettinger6023d332016-11-21 15:32:08 -0800853 n = 104729
Raymond Hettinger30d00e52016-10-29 16:55:36 -0700854
855 self.gen.seed(8675309)
856 a = self.gen.choices(range(n), k=10000)
857
858 self.gen.seed(8675309)
859 b = self.gen.choices(range(n), [1]*n, k=10000)
860 self.assertEqual(a, b)
861
862 self.gen.seed(8675309)
863 c = self.gen.choices(range(n), cum_weights=range(1, n+1), k=10000)
864 self.assertEqual(a, c)
865
penguindustin96466302019-05-06 14:57:17 -0400866 # American Roulette
Raymond Hettinger77d574d2016-10-29 17:42:36 -0700867 population = ['Red', 'Black', 'Green']
868 weights = [18, 18, 2]
869 cum_weights = [18, 36, 38]
870 expanded_population = ['Red'] * 18 + ['Black'] * 18 + ['Green'] * 2
871
872 self.gen.seed(9035768)
873 a = self.gen.choices(expanded_population, k=10000)
874
875 self.gen.seed(9035768)
876 b = self.gen.choices(population, weights, k=10000)
877 self.assertEqual(a, b)
878
879 self.gen.seed(9035768)
880 c = self.gen.choices(population, cum_weights=cum_weights, k=10000)
881 self.assertEqual(a, c)
882
Victor Stinner9f5fe792020-04-17 19:05:35 +0200883 def test_randbytes(self):
884 super().test_randbytes()
885
886 # Mersenne Twister randbytes() is deterministic
887 # and does not depend on the endian and bitness.
888 seed = 8675309
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300889 expected = b'3\xa8\xf9f\xf4\xa4\xd06\x19\x8f\x9f\x82\x02oe\xf0'
Victor Stinner9f5fe792020-04-17 19:05:35 +0200890
891 self.gen.seed(seed)
892 self.assertEqual(self.gen.randbytes(16), expected)
893
894 # randbytes(0) must not consume any entropy
895 self.gen.seed(seed)
896 self.assertEqual(self.gen.randbytes(0), b'')
897 self.assertEqual(self.gen.randbytes(16), expected)
898
899 # Four randbytes(4) calls give the same output than randbytes(16)
900 self.gen.seed(seed)
901 self.assertEqual(b''.join([self.gen.randbytes(4) for _ in range(4)]),
902 expected)
903
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300904 # Each randbytes(1), randbytes(2) or randbytes(3) call consumes
905 # 4 bytes of entropy
Victor Stinner9f5fe792020-04-17 19:05:35 +0200906 self.gen.seed(seed)
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300907 expected1 = expected[3::4]
908 self.assertEqual(b''.join(self.gen.randbytes(1) for _ in range(4)),
909 expected1)
910
911 self.gen.seed(seed)
912 expected2 = b''.join(expected[i + 2: i + 4]
Victor Stinner9f5fe792020-04-17 19:05:35 +0200913 for i in range(0, len(expected), 4))
914 self.assertEqual(b''.join(self.gen.randbytes(2) for _ in range(4)),
915 expected2)
916
917 self.gen.seed(seed)
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300918 expected3 = b''.join(expected[i + 1: i + 4]
Victor Stinner9f5fe792020-04-17 19:05:35 +0200919 for i in range(0, len(expected), 4))
920 self.assertEqual(b''.join(self.gen.randbytes(3) for _ in range(4)),
921 expected3)
922
Serhiy Storchaka223221b2020-04-17 23:51:28 +0300923 def test_randbytes_getrandbits(self):
924 # There is a simple relation between randbytes() and getrandbits()
925 seed = 2849427419
926 gen2 = random.Random()
927 self.gen.seed(seed)
928 gen2.seed(seed)
929 for n in range(9):
930 self.assertEqual(self.gen.randbytes(n),
931 gen2.getrandbits(n * 8).to_bytes(n, 'little'))
932
jonanifrancof7b5bacd2021-01-18 19:04:29 +0100933 def test_sample_counts_equivalence(self):
934 # Test the documented strong equivalence to a sample with repeated elements.
935 # We run this test on random.Random() which makes deterministic selections
936 # for a given seed value.
937 sample = self.gen.sample
938 seed = self.gen.seed
939
940 colors = ['red', 'green', 'blue', 'orange', 'black', 'amber']
941 counts = [500, 200, 20, 10, 5, 1 ]
942 k = 700
943 seed(8675309)
944 s1 = sample(colors, counts=counts, k=k)
945 seed(8675309)
946 expanded = [color for (color, count) in zip(colors, counts) for i in range(count)]
947 self.assertEqual(len(expanded), sum(counts))
948 s2 = sample(expanded, k=k)
949 self.assertEqual(s1, s2)
950
951 pop = 'abcdefghi'
952 counts = [10, 9, 8, 7, 6, 5, 4, 3, 2]
953 seed(8675309)
954 s1 = ''.join(sample(pop, counts=counts, k=30))
955 expanded = ''.join([letter for (letter, count) in zip(pop, counts) for i in range(count)])
956 seed(8675309)
957 s2 = ''.join(sample(expanded, k=30))
958 self.assertEqual(s1, s2)
959
Victor Stinner9f5fe792020-04-17 19:05:35 +0200960
Raymond Hettinger2d0c2562009-02-19 09:53:18 +0000961def gamma(z, sqrt2pi=(2.0*pi)**0.5):
962 # Reflection to right half of complex plane
963 if z < 0.5:
964 return pi / sin(pi*z) / gamma(1.0-z)
965 # Lanczos approximation with g=7
966 az = z + (7.0 - 0.5)
967 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([
968 0.9999999999995183,
969 676.5203681218835 / z,
970 -1259.139216722289 / (z+1.0),
971 771.3234287757674 / (z+2.0),
972 -176.6150291498386 / (z+3.0),
973 12.50734324009056 / (z+4.0),
974 -0.1385710331296526 / (z+5.0),
975 0.9934937113930748e-05 / (z+6.0),
976 0.1659470187408462e-06 / (z+7.0),
977 ])
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000978
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000979class TestDistributions(unittest.TestCase):
980 def test_zeroinputs(self):
981 # Verify that distributions can handle a series of zero inputs'
982 g = random.Random()
Guido van Rossum805365e2007-05-07 22:24:25 +0000983 x = [g.random() for i in range(50)] + [0.0]*5
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000984 g.random = x[:].pop; g.uniform(1,10)
985 g.random = x[:].pop; g.paretovariate(1.0)
986 g.random = x[:].pop; g.expovariate(1.0)
987 g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +0200988 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000989 g.random = x[:].pop; g.normalvariate(0.0, 1.0)
990 g.random = x[:].pop; g.gauss(0.0, 1.0)
991 g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
992 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0)
993 g.random = x[:].pop; g.gammavariate(0.01, 1.0)
994 g.random = x[:].pop; g.gammavariate(1.0, 1.0)
995 g.random = x[:].pop; g.gammavariate(200.0, 1.0)
996 g.random = x[:].pop; g.betavariate(3.0, 3.0)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000997 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0)
Raymond Hettinger15ec3732003-01-05 01:08:34 +0000998
Raymond Hettinger3dd990c2003-01-05 09:20:06 +0000999 def test_avg_std(self):
1000 # Use integration to test distribution average and standard deviation.
1001 # Only works for distributions which do not consume variates in pairs
1002 g = random.Random()
1003 N = 5000
Guido van Rossum805365e2007-05-07 22:24:25 +00001004 x = [i/float(N) for i in range(1,N)]
Raymond Hettinger3dd990c2003-01-05 09:20:06 +00001005 for variate, args, mu, sigmasqrd in [
1006 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
Christian Heimesfe337bf2008-03-23 21:54:12 +00001007 (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 +00001008 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +02001009 (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
Raymond Hettinger3dd990c2003-01-05 09:20:06 +00001010 (g.paretovariate, (5.0,), 5.0/(5.0-1),
1011 5.0/((5.0-1)**2*(5.0-2))),
1012 (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
1013 gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]:
1014 g.random = x[:].pop
1015 y = []
Guido van Rossum805365e2007-05-07 22:24:25 +00001016 for i in range(len(x)):
Raymond Hettinger3dd990c2003-01-05 09:20:06 +00001017 try:
1018 y.append(variate(*args))
1019 except IndexError:
1020 pass
1021 s1 = s2 = 0
1022 for e in y:
1023 s1 += e
1024 s2 += (e - mu) ** 2
1025 N = len(y)
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +02001026 self.assertAlmostEqual(s1/N, mu, places=2,
1027 msg='%s%r' % (variate.__name__, args))
1028 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
1029 msg='%s%r' % (variate.__name__, args))
1030
1031 def test_constant(self):
1032 g = random.Random()
1033 N = 100
1034 for variate, args, expected in [
1035 (g.uniform, (10.0, 10.0), 10.0),
1036 (g.triangular, (10.0, 10.0), 10.0),
Raymond Hettinger978c6ab2014-05-25 17:25:27 -07001037 (g.triangular, (10.0, 10.0, 10.0), 10.0),
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +02001038 (g.expovariate, (float('inf'),), 0.0),
1039 (g.vonmisesvariate, (3.0, float('inf')), 3.0),
1040 (g.gauss, (10.0, 0.0), 10.0),
1041 (g.lognormvariate, (0.0, 0.0), 1.0),
1042 (g.lognormvariate, (-float('inf'), 0.0), 0.0),
1043 (g.normalvariate, (10.0, 0.0), 10.0),
1044 (g.paretovariate, (float('inf'),), 1.0),
1045 (g.weibullvariate, (10.0, float('inf')), 10.0),
1046 (g.weibullvariate, (0.0, 10.0), 0.0),
1047 ]:
1048 for i in range(N):
1049 self.assertEqual(variate(*args), expected)
Raymond Hettinger3dd990c2003-01-05 09:20:06 +00001050
Mark Dickinsonbe5f9192013-02-10 14:16:10 +00001051 def test_von_mises_range(self):
1052 # Issue 17149: von mises variates were not consistently in the
1053 # range [0, 2*PI].
1054 g = random.Random()
1055 N = 100
1056 for mu in 0.0, 0.1, 3.1, 6.2:
1057 for kappa in 0.0, 2.3, 500.0:
1058 for _ in range(N):
1059 sample = g.vonmisesvariate(mu, kappa)
1060 self.assertTrue(
1061 0 <= sample <= random.TWOPI,
1062 msg=("vonmisesvariate({}, {}) produced a result {} out"
1063 " of range [0, 2*pi]").format(mu, kappa, sample))
1064
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +02001065 def test_von_mises_large_kappa(self):
1066 # Issue #17141: vonmisesvariate() was hang for large kappas
1067 random.vonmisesvariate(0, 1e15)
1068 random.vonmisesvariate(0, 1e100)
1069
R David Murraye3e1c172013-04-02 12:47:23 -04001070 def test_gammavariate_errors(self):
1071 # Both alpha and beta must be > 0.0
1072 self.assertRaises(ValueError, random.gammavariate, -1, 3)
1073 self.assertRaises(ValueError, random.gammavariate, 0, 2)
1074 self.assertRaises(ValueError, random.gammavariate, 2, 0)
1075 self.assertRaises(ValueError, random.gammavariate, 1, -3)
1076
leodema63d15222018-12-24 07:54:25 +01001077 # There are three different possibilities in the current implementation
1078 # of random.gammavariate(), depending on the value of 'alpha'. What we
1079 # are going to do here is to fix the values returned by random() to
1080 # generate test cases that provide 100% line coverage of the method.
R David Murraye3e1c172013-04-02 12:47:23 -04001081 @unittest.mock.patch('random.Random.random')
leodema63d15222018-12-24 07:54:25 +01001082 def test_gammavariate_alpha_greater_one(self, random_mock):
R David Murraye3e1c172013-04-02 12:47:23 -04001083
leodema63d15222018-12-24 07:54:25 +01001084 # #1: alpha > 1.0.
1085 # We want the first random number to be outside the
R David Murraye3e1c172013-04-02 12:47:23 -04001086 # [1e-7, .9999999] range, so that the continue statement executes
1087 # once. The values of u1 and u2 will be 0.5 and 0.3, respectively.
1088 random_mock.side_effect = [1e-8, 0.5, 0.3]
1089 returned_value = random.gammavariate(1.1, 2.3)
1090 self.assertAlmostEqual(returned_value, 2.53)
1091
leodema63d15222018-12-24 07:54:25 +01001092 @unittest.mock.patch('random.Random.random')
1093 def test_gammavariate_alpha_equal_one(self, random_mock):
R David Murraye3e1c172013-04-02 12:47:23 -04001094
leodema63d15222018-12-24 07:54:25 +01001095 # #2.a: alpha == 1.
1096 # The execution body of the while loop executes once.
1097 # Then random.random() returns 0.45,
1098 # which causes while to stop looping and the algorithm to terminate.
1099 random_mock.side_effect = [0.45]
1100 returned_value = random.gammavariate(1.0, 3.14)
1101 self.assertAlmostEqual(returned_value, 1.877208182372648)
1102
1103 @unittest.mock.patch('random.Random.random')
1104 def test_gammavariate_alpha_equal_one_equals_expovariate(self, random_mock):
1105
1106 # #2.b: alpha == 1.
1107 # It must be equivalent of calling expovariate(1.0 / beta).
1108 beta = 3.14
1109 random_mock.side_effect = [1e-8, 1e-8]
1110 gammavariate_returned_value = random.gammavariate(1.0, beta)
1111 expovariate_returned_value = random.expovariate(1.0 / beta)
1112 self.assertAlmostEqual(gammavariate_returned_value, expovariate_returned_value)
1113
1114 @unittest.mock.patch('random.Random.random')
1115 def test_gammavariate_alpha_between_zero_and_one(self, random_mock):
1116
1117 # #3: 0 < alpha < 1.
1118 # This is the most complex region of code to cover,
R David Murraye3e1c172013-04-02 12:47:23 -04001119 # as there are multiple if-else statements. Let's take a look at the
1120 # source code, and determine the values that we need accordingly:
1121 #
1122 # while 1:
1123 # u = random()
1124 # b = (_e + alpha)/_e
1125 # p = b*u
1126 # if p <= 1.0: # <=== (A)
1127 # x = p ** (1.0/alpha)
1128 # else: # <=== (B)
1129 # x = -_log((b-p)/alpha)
1130 # u1 = random()
1131 # if p > 1.0: # <=== (C)
1132 # if u1 <= x ** (alpha - 1.0): # <=== (D)
1133 # break
1134 # elif u1 <= _exp(-x): # <=== (E)
1135 # break
1136 # return x * beta
1137 #
1138 # First, we want (A) to be True. For that we need that:
1139 # b*random() <= 1.0
1140 # r1 = random() <= 1.0 / b
1141 #
1142 # We now get to the second if-else branch, and here, since p <= 1.0,
1143 # (C) is False and we take the elif branch, (E). For it to be True,
1144 # so that the break is executed, we need that:
1145 # r2 = random() <= _exp(-x)
1146 # r2 <= _exp(-(p ** (1.0/alpha)))
1147 # r2 <= _exp(-((b*r1) ** (1.0/alpha)))
1148
1149 _e = random._e
1150 _exp = random._exp
1151 _log = random._log
1152 alpha = 0.35
1153 beta = 1.45
1154 b = (_e + alpha)/_e
1155 epsilon = 0.01
1156
1157 r1 = 0.8859296441566 # 1.0 / b
1158 r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha)))
1159
1160 # These four "random" values result in the following trace:
1161 # (A) True, (E) False --> [next iteration of while]
1162 # (A) True, (E) True --> [while loop breaks]
1163 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
1164 returned_value = random.gammavariate(alpha, beta)
1165 self.assertAlmostEqual(returned_value, 1.4499999999997544)
1166
1167 # Let's now make (A) be False. If this is the case, when we get to the
1168 # second if-else 'p' is greater than 1, so (C) evaluates to True. We
1169 # now encounter a second if statement, (D), which in order to execute
1170 # must satisfy the following condition:
1171 # r2 <= x ** (alpha - 1.0)
1172 # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0)
1173 # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0)
1174 r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False
1175 r2 = 0.9445400408898141
1176
1177 # And these four values result in the following trace:
1178 # (B) and (C) True, (D) False --> [next iteration of while]
1179 # (B) and (C) True, (D) True [while loop breaks]
1180 random_mock.side_effect = [r1, r2 + epsilon, r1, r2]
1181 returned_value = random.gammavariate(alpha, beta)
1182 self.assertAlmostEqual(returned_value, 1.5830349561760781)
1183
1184 @unittest.mock.patch('random.Random.gammavariate')
1185 def test_betavariate_return_zero(self, gammavariate_mock):
1186 # betavariate() returns zero when the Gamma distribution
1187 # that it uses internally returns this same value.
1188 gammavariate_mock.return_value = 0.0
1189 self.assertEqual(0.0, random.betavariate(2.71828, 3.14159))
Serhiy Storchaka6c22b1d2013-02-10 19:28:56 +02001190
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001191
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001192class TestRandomSubclassing(unittest.TestCase):
1193 def test_random_subclass_with_kwargs(self):
1194 # SF bug #1486663 -- this used to erroneously raise a TypeError
1195 class Subclass(random.Random):
1196 def __init__(self, newarg=None):
1197 random.Random.__init__(self)
1198 Subclass(newarg=1)
1199
1200 def test_subclasses_overriding_methods(self):
1201 # Subclasses with an overridden random, but only the original
1202 # getrandbits method should not rely on getrandbits in for randrange,
1203 # but should use a getrandbits-independent implementation instead.
1204
1205 # subclass providing its own random **and** getrandbits methods
1206 # like random.SystemRandom does => keep relying on getrandbits for
1207 # randrange
1208 class SubClass1(random.Random):
1209 def random(self):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001210 called.add('SubClass1.random')
1211 return random.Random.random(self)
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001212
1213 def getrandbits(self, n):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001214 called.add('SubClass1.getrandbits')
1215 return random.Random.getrandbits(self, n)
1216 called = set()
1217 SubClass1().randrange(42)
1218 self.assertEqual(called, {'SubClass1.getrandbits'})
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001219
1220 # subclass providing only random => can only use random for randrange
1221 class SubClass2(random.Random):
1222 def random(self):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001223 called.add('SubClass2.random')
1224 return random.Random.random(self)
1225 called = set()
1226 SubClass2().randrange(42)
1227 self.assertEqual(called, {'SubClass2.random'})
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001228
1229 # subclass defining getrandbits to complement its inherited random
1230 # => can now rely on getrandbits for randrange again
1231 class SubClass3(SubClass2):
1232 def getrandbits(self, n):
Serhiy Storchakaec1622d2018-05-08 15:45:15 +03001233 called.add('SubClass3.getrandbits')
1234 return random.Random.getrandbits(self, n)
1235 called = set()
1236 SubClass3().randrange(42)
1237 self.assertEqual(called, {'SubClass3.getrandbits'})
1238
1239 # subclass providing only random and inherited getrandbits
1240 # => random takes precedence
1241 class SubClass4(SubClass3):
1242 def random(self):
1243 called.add('SubClass4.random')
1244 return random.Random.random(self)
1245 called = set()
1246 SubClass4().randrange(42)
1247 self.assertEqual(called, {'SubClass4.random'})
1248
1249 # Following subclasses don't define random or getrandbits directly,
1250 # but inherit them from classes which are not subclasses of Random
1251 class Mixin1:
1252 def random(self):
1253 called.add('Mixin1.random')
1254 return random.Random.random(self)
1255 class Mixin2:
1256 def getrandbits(self, n):
1257 called.add('Mixin2.getrandbits')
1258 return random.Random.getrandbits(self, n)
1259
1260 class SubClass5(Mixin1, random.Random):
1261 pass
1262 called = set()
1263 SubClass5().randrange(42)
1264 self.assertEqual(called, {'Mixin1.random'})
1265
1266 class SubClass6(Mixin2, random.Random):
1267 pass
1268 called = set()
1269 SubClass6().randrange(42)
1270 self.assertEqual(called, {'Mixin2.getrandbits'})
1271
1272 class SubClass7(Mixin1, Mixin2, random.Random):
1273 pass
1274 called = set()
1275 SubClass7().randrange(42)
1276 self.assertEqual(called, {'Mixin1.random'})
1277
1278 class SubClass8(Mixin2, Mixin1, random.Random):
1279 pass
1280 called = set()
1281 SubClass8().randrange(42)
1282 self.assertEqual(called, {'Mixin2.getrandbits'})
1283
Wolfgang Maierba3a87a2018-04-17 17:16:17 +02001284
Raymond Hettinger40f62172002-12-29 23:03:38 +00001285class TestModule(unittest.TestCase):
1286 def testMagicConstants(self):
1287 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
1288 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
1289 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
1290 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
1291
1292 def test__all__(self):
1293 # tests validity but not completeness of the __all__ list
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001294 self.assertTrue(set(random.__all__) <= set(dir(random)))
Raymond Hettinger40f62172002-12-29 23:03:38 +00001295
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001296 @unittest.skipUnless(hasattr(os, "fork"), "fork() required")
1297 def test_after_fork(self):
1298 # Test the global Random instance gets reseeded in child
1299 r, w = os.pipe()
Victor Stinnerda5e9302017-08-09 17:59:05 +02001300 pid = os.fork()
1301 if pid == 0:
1302 # child process
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001303 try:
1304 val = random.getrandbits(128)
1305 with open(w, "w") as f:
1306 f.write(str(val))
1307 finally:
1308 os._exit(0)
1309 else:
Victor Stinnerda5e9302017-08-09 17:59:05 +02001310 # parent process
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001311 os.close(w)
1312 val = random.getrandbits(128)
1313 with open(r, "r") as f:
1314 child_val = eval(f.read())
1315 self.assertNotEqual(val, child_val)
1316
Victor Stinner278c1e12020-03-31 20:08:12 +02001317 support.wait_process(pid, exitcode=0)
Victor Stinnerda5e9302017-08-09 17:59:05 +02001318
Thomas Woutersb2137042007-02-01 18:02:27 +00001319
Raymond Hettinger40f62172002-12-29 23:03:38 +00001320if __name__ == "__main__":
Ezio Melotti3e4a98b2013-04-19 05:45:27 +03001321 unittest.main()