Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1 | import unittest |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 2 | import unittest.mock |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 3 | import random |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 4 | import os |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 5 | import time |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 6 | import pickle |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 7 | import warnings |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 8 | from functools import partial |
Victor Stinner | bd1b49a | 2016-10-19 10:11:37 +0200 | [diff] [blame] | 9 | from math import log, exp, pi, fsum, sin, factorial |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import support |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 11 | from fractions import Fraction |
Raymond Hettinger | 81a5fc3 | 2020-05-08 07:53:15 -0700 | [diff] [blame^] | 12 | from collections import Counter |
csabella | f111fd2 | 2017-05-11 11:19:35 -0400 | [diff] [blame] | 13 | |
Ezio Melotti | 3e4a98b | 2013-04-19 05:45:27 +0300 | [diff] [blame] | 14 | class TestBasicOps: |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 15 | # Superclass with tests common to all generators. |
| 16 | # Subclasses must arrange for self.gen to retrieve the Random instance |
| 17 | # to be tested. |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 18 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 19 | def randomlist(self, n): |
| 20 | """Helper function to make a list of random numbers""" |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 21 | return [self.gen.random() for i in range(n)] |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 22 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 23 | def test_autoseed(self): |
| 24 | self.gen.seed() |
| 25 | state1 = self.gen.getstate() |
Raymond Hettinger | 3081d59 | 2003-08-09 18:30:57 +0000 | [diff] [blame] | 26 | time.sleep(0.1) |
Mike | 53f7a7c | 2017-12-14 14:04:53 +0300 | [diff] [blame] | 27 | self.gen.seed() # different seeds at different times |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 28 | state2 = self.gen.getstate() |
| 29 | self.assertNotEqual(state1, state2) |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 31 | def test_saverestore(self): |
| 32 | N = 1000 |
| 33 | self.gen.seed() |
| 34 | state = self.gen.getstate() |
| 35 | randseq = self.randomlist(N) |
| 36 | self.gen.setstate(state) # should regenerate the same sequence |
| 37 | self.assertEqual(randseq, self.randomlist(N)) |
| 38 | |
| 39 | def test_seedargs(self): |
Mark Dickinson | 95aeae0 | 2012-06-24 11:05:30 +0100 | [diff] [blame] | 40 | # Seed value with a negative hash. |
| 41 | class MySeed(object): |
| 42 | def __hash__(self): |
| 43 | return -1729 |
Xtreak | a06d683 | 2019-09-12 09:13:20 +0100 | [diff] [blame] | 44 | for arg in [None, 0, 1, -1, 10**20, -(10**20), |
Victor Stinner | 00d7cd8 | 2020-03-10 15:15:14 +0100 | [diff] [blame] | 45 | False, True, 3.14, 'a']: |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 46 | self.gen.seed(arg) |
Xtreak | a06d683 | 2019-09-12 09:13:20 +0100 | [diff] [blame] | 47 | |
| 48 | for arg in [1+2j, tuple('abc'), MySeed()]: |
| 49 | with self.assertWarns(DeprecationWarning): |
| 50 | self.gen.seed(arg) |
| 51 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 52 | for arg in [list(range(3)), dict(one=1)]: |
Xtreak | a06d683 | 2019-09-12 09:13:20 +0100 | [diff] [blame] | 53 | with self.assertWarns(DeprecationWarning): |
| 54 | self.assertRaises(TypeError, self.gen.seed, arg) |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 55 | self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4) |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 56 | self.assertRaises(TypeError, type(self.gen), []) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 57 | |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 58 | @unittest.mock.patch('random._urandom') # os.urandom |
| 59 | def test_seed_when_randomness_source_not_found(self, urandom_mock): |
| 60 | # Random.seed() uses time.time() when an operating system specific |
csabella | f111fd2 | 2017-05-11 11:19:35 -0400 | [diff] [blame] | 61 | # randomness source is not found. To test this on machines where it |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 62 | # exists, run the above test, test_seedargs(), again after mocking |
| 63 | # os.urandom() so that it raises the exception expected when the |
| 64 | # randomness source is not available. |
| 65 | urandom_mock.side_effect = NotImplementedError |
| 66 | self.test_seedargs() |
| 67 | |
Antoine Pitrou | 5e39433 | 2012-11-04 02:10:33 +0100 | [diff] [blame] | 68 | def test_shuffle(self): |
| 69 | shuffle = self.gen.shuffle |
| 70 | lst = [] |
| 71 | shuffle(lst) |
| 72 | self.assertEqual(lst, []) |
| 73 | lst = [37] |
| 74 | shuffle(lst) |
| 75 | self.assertEqual(lst, [37]) |
| 76 | seqs = [list(range(n)) for n in range(10)] |
| 77 | shuffled_seqs = [list(range(n)) for n in range(10)] |
| 78 | for shuffled_seq in shuffled_seqs: |
| 79 | shuffle(shuffled_seq) |
| 80 | for (seq, shuffled_seq) in zip(seqs, shuffled_seqs): |
| 81 | self.assertEqual(len(seq), len(shuffled_seq)) |
| 82 | self.assertEqual(set(seq), set(shuffled_seq)) |
Antoine Pitrou | 5e39433 | 2012-11-04 02:10:33 +0100 | [diff] [blame] | 83 | # The above tests all would pass if the shuffle was a |
| 84 | # no-op. The following non-deterministic test covers that. It |
| 85 | # asserts that the shuffled sequence of 1000 distinct elements |
| 86 | # must be different from the original one. Although there is |
| 87 | # mathematically a non-zero probability that this could |
| 88 | # actually happen in a genuinely random shuffle, it is |
| 89 | # completely negligible, given that the number of possible |
| 90 | # permutations of 1000 objects is 1000! (factorial of 1000), |
| 91 | # which is considerably larger than the number of atoms in the |
| 92 | # universe... |
| 93 | lst = list(range(1000)) |
| 94 | shuffled_lst = list(range(1000)) |
| 95 | shuffle(shuffled_lst) |
| 96 | self.assertTrue(lst != shuffled_lst) |
| 97 | shuffle(lst) |
| 98 | self.assertTrue(lst != shuffled_lst) |
csabella | f111fd2 | 2017-05-11 11:19:35 -0400 | [diff] [blame] | 99 | self.assertRaises(TypeError, shuffle, (1, 2, 3)) |
| 100 | |
| 101 | def test_shuffle_random_argument(self): |
| 102 | # Test random argument to shuffle. |
| 103 | shuffle = self.gen.shuffle |
| 104 | mock_random = unittest.mock.Mock(return_value=0.5) |
| 105 | seq = bytearray(b'abcdefghijk') |
Raymond Hettinger | 190fac9 | 2020-05-02 16:45:32 -0700 | [diff] [blame] | 106 | with self.assertWarns(DeprecationWarning): |
| 107 | shuffle(seq, mock_random) |
csabella | f111fd2 | 2017-05-11 11:19:35 -0400 | [diff] [blame] | 108 | mock_random.assert_called_with() |
Antoine Pitrou | 5e39433 | 2012-11-04 02:10:33 +0100 | [diff] [blame] | 109 | |
Raymond Hettinger | dc4872e | 2010-09-07 10:06:56 +0000 | [diff] [blame] | 110 | def test_choice(self): |
| 111 | choice = self.gen.choice |
| 112 | with self.assertRaises(IndexError): |
| 113 | choice([]) |
| 114 | self.assertEqual(choice([50]), 50) |
| 115 | self.assertIn(choice([25, 75]), [25, 75]) |
| 116 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 117 | def test_sample(self): |
| 118 | # For the entire allowable range of 0 <= k <= N, validate that |
| 119 | # the sample is of the correct length and contains only unique items |
| 120 | N = 100 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 121 | population = range(N) |
| 122 | for k in range(N+1): |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 123 | s = self.gen.sample(population, k) |
| 124 | self.assertEqual(len(s), k) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 125 | uniq = set(s) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 126 | self.assertEqual(len(uniq), k) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 127 | self.assertTrue(uniq <= set(population)) |
Raymond Hettinger | 8ec7881 | 2003-01-04 05:55:11 +0000 | [diff] [blame] | 128 | self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 129 | # Exception raised if size of sample exceeds that of population |
| 130 | self.assertRaises(ValueError, self.gen.sample, population, N+1) |
Raymond Hettinger | bf87126 | 2016-11-21 14:34:33 -0800 | [diff] [blame] | 131 | self.assertRaises(ValueError, self.gen.sample, [], -1) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 132 | |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 133 | def test_sample_distribution(self): |
| 134 | # For the entire allowable range of 0 <= k <= N, validate that |
| 135 | # sample generates all possible permutations |
| 136 | n = 5 |
| 137 | pop = range(n) |
| 138 | trials = 10000 # large num prevents false negatives without slowing normal case |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 139 | for k in range(n): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 140 | expected = factorial(n) // factorial(n-k) |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 141 | perms = {} |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 142 | for i in range(trials): |
Raymond Hettinger | 7b0cf76 | 2003-01-17 17:23:23 +0000 | [diff] [blame] | 143 | perms[tuple(self.gen.sample(pop, k))] = None |
| 144 | if len(perms) == expected: |
| 145 | break |
| 146 | else: |
| 147 | self.fail() |
| 148 | |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 149 | def test_sample_inputs(self): |
| 150 | # SF bug #801342 -- population can be any iterable defining __len__() |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 151 | self.gen.sample(range(20), 2) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 152 | self.gen.sample(range(20), 2) |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 153 | self.gen.sample(str('abcdefghijklmnopqrst'), 2) |
| 154 | self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) |
| 155 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 156 | def test_sample_on_dicts(self): |
Raymond Hettinger | 1acde19 | 2008-01-14 01:00:53 +0000 | [diff] [blame] | 157 | self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 158 | |
Raymond Hettinger | 4fe0020 | 2020-04-19 00:36:42 -0700 | [diff] [blame] | 159 | def test_sample_on_sets(self): |
| 160 | with self.assertWarns(DeprecationWarning): |
| 161 | population = {10, 20, 30, 40, 50, 60, 70} |
| 162 | self.gen.sample(population, k=5) |
| 163 | |
Raymond Hettinger | 81a5fc3 | 2020-05-08 07:53:15 -0700 | [diff] [blame^] | 164 | def test_sample_with_counts(self): |
| 165 | sample = self.gen.sample |
| 166 | |
| 167 | # General case |
| 168 | colors = ['red', 'green', 'blue', 'orange', 'black', 'brown', 'amber'] |
| 169 | counts = [500, 200, 20, 10, 5, 0, 1 ] |
| 170 | k = 700 |
| 171 | summary = Counter(sample(colors, counts=counts, k=k)) |
| 172 | self.assertEqual(sum(summary.values()), k) |
| 173 | for color, weight in zip(colors, counts): |
| 174 | self.assertLessEqual(summary[color], weight) |
| 175 | self.assertNotIn('brown', summary) |
| 176 | |
| 177 | # Case that exhausts the population |
| 178 | k = sum(counts) |
| 179 | summary = Counter(sample(colors, counts=counts, k=k)) |
| 180 | self.assertEqual(sum(summary.values()), k) |
| 181 | for color, weight in zip(colors, counts): |
| 182 | self.assertLessEqual(summary[color], weight) |
| 183 | self.assertNotIn('brown', summary) |
| 184 | |
| 185 | # Case with population size of 1 |
| 186 | summary = Counter(sample(['x'], counts=[10], k=8)) |
| 187 | self.assertEqual(summary, Counter(x=8)) |
| 188 | |
| 189 | # Case with all counts equal. |
| 190 | nc = len(colors) |
| 191 | summary = Counter(sample(colors, counts=[10]*nc, k=10*nc)) |
| 192 | self.assertEqual(summary, Counter(10*colors)) |
| 193 | |
| 194 | # Test error handling |
| 195 | with self.assertRaises(TypeError): |
| 196 | sample(['red', 'green', 'blue'], counts=10, k=10) # counts not iterable |
| 197 | with self.assertRaises(ValueError): |
| 198 | sample(['red', 'green', 'blue'], counts=[-3, -7, -8], k=2) # counts are negative |
| 199 | with self.assertRaises(ValueError): |
| 200 | sample(['red', 'green', 'blue'], counts=[0, 0, 0], k=2) # counts are zero |
| 201 | with self.assertRaises(ValueError): |
| 202 | sample(['red', 'green'], counts=[10, 10], k=21) # population too small |
| 203 | with self.assertRaises(ValueError): |
| 204 | sample(['red', 'green', 'blue'], counts=[1, 2], k=2) # too few counts |
| 205 | with self.assertRaises(ValueError): |
| 206 | sample(['red', 'green', 'blue'], counts=[1, 2, 3, 4], k=2) # too many counts |
| 207 | |
| 208 | def test_sample_counts_equivalence(self): |
| 209 | # Test the documented strong equivalence to a sample with repeated elements. |
| 210 | # We run this test on random.Random() which makes deterministic selections |
| 211 | # for a given seed value. |
| 212 | sample = random.sample |
| 213 | seed = random.seed |
| 214 | |
| 215 | colors = ['red', 'green', 'blue', 'orange', 'black', 'amber'] |
| 216 | counts = [500, 200, 20, 10, 5, 1 ] |
| 217 | k = 700 |
| 218 | seed(8675309) |
| 219 | s1 = sample(colors, counts=counts, k=k) |
| 220 | seed(8675309) |
| 221 | expanded = [color for (color, count) in zip(colors, counts) for i in range(count)] |
| 222 | self.assertEqual(len(expanded), sum(counts)) |
| 223 | s2 = sample(expanded, k=k) |
| 224 | self.assertEqual(s1, s2) |
| 225 | |
| 226 | pop = 'abcdefghi' |
| 227 | counts = [10, 9, 8, 7, 6, 5, 4, 3, 2] |
| 228 | seed(8675309) |
| 229 | s1 = ''.join(sample(pop, counts=counts, k=30)) |
| 230 | expanded = ''.join([letter for (letter, count) in zip(pop, counts) for i in range(count)]) |
| 231 | seed(8675309) |
| 232 | s2 = ''.join(sample(expanded, k=30)) |
| 233 | self.assertEqual(s1, s2) |
| 234 | |
Raymond Hettinger | 28aa4a0 | 2016-09-07 00:08:44 -0700 | [diff] [blame] | 235 | def test_choices(self): |
| 236 | choices = self.gen.choices |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 237 | data = ['red', 'green', 'blue', 'yellow'] |
| 238 | str_data = 'abcd' |
| 239 | range_data = range(4) |
| 240 | set_data = set(range(4)) |
| 241 | |
| 242 | # basic functionality |
| 243 | for sample in [ |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 244 | choices(data, k=5), |
| 245 | choices(data, range(4), k=5), |
Raymond Hettinger | 28aa4a0 | 2016-09-07 00:08:44 -0700 | [diff] [blame] | 246 | choices(k=5, population=data, weights=range(4)), |
| 247 | choices(k=5, population=data, cum_weights=range(4)), |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 248 | ]: |
| 249 | self.assertEqual(len(sample), 5) |
| 250 | self.assertEqual(type(sample), list) |
| 251 | self.assertTrue(set(sample) <= set(data)) |
| 252 | |
| 253 | # test argument handling |
Raymond Hettinger | 28aa4a0 | 2016-09-07 00:08:44 -0700 | [diff] [blame] | 254 | with self.assertRaises(TypeError): # missing arguments |
| 255 | choices(2) |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 256 | |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 257 | self.assertEqual(choices(data, k=0), []) # k == 0 |
| 258 | self.assertEqual(choices(data, k=-1), []) # negative k behaves like ``[0] * -1`` |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 259 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 260 | choices(data, k=2.5) # k is a float |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 261 | |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 262 | self.assertTrue(set(choices(str_data, k=5)) <= set(str_data)) # population is a string sequence |
| 263 | self.assertTrue(set(choices(range_data, k=5)) <= set(range_data)) # population is a range |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 264 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 265 | choices(set_data, k=2) # population is not a sequence |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 266 | |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 267 | self.assertTrue(set(choices(data, None, k=5)) <= set(data)) # weights is None |
| 268 | self.assertTrue(set(choices(data, weights=None, k=5)) <= set(data)) |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 269 | with self.assertRaises(ValueError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 270 | choices(data, [1,2], k=5) # len(weights) != len(population) |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 271 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 272 | choices(data, 10, k=5) # non-iterable weights |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 273 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 274 | choices(data, [None]*4, k=5) # non-numeric weights |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 275 | for weights in [ |
| 276 | [15, 10, 25, 30], # integer weights |
| 277 | [15.1, 10.2, 25.2, 30.3], # float weights |
| 278 | [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights |
| 279 | [True, False, True, False] # booleans (include / exclude) |
| 280 | ]: |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 281 | self.assertTrue(set(choices(data, weights, k=5)) <= set(data)) |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 282 | |
| 283 | with self.assertRaises(ValueError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 284 | choices(data, cum_weights=[1,2], k=5) # len(weights) != len(population) |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 285 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 286 | choices(data, cum_weights=10, k=5) # non-iterable cum_weights |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 287 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 288 | choices(data, cum_weights=[None]*4, k=5) # non-numeric cum_weights |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 289 | with self.assertRaises(TypeError): |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 290 | choices(data, range(4), cum_weights=range(4), k=5) # both weights and cum_weights |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 291 | for weights in [ |
| 292 | [15, 10, 25, 30], # integer cum_weights |
| 293 | [15.1, 10.2, 25.2, 30.3], # float cum_weights |
| 294 | [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional cum_weights |
| 295 | ]: |
Raymond Hettinger | 9016f28 | 2016-09-26 21:45:57 -0700 | [diff] [blame] | 296 | self.assertTrue(set(choices(data, cum_weights=weights, k=5)) <= set(data)) |
Raymond Hettinger | e8f1e00 | 2016-09-06 17:15:29 -0700 | [diff] [blame] | 297 | |
Raymond Hettinger | 7b16652 | 2016-10-14 01:19:38 -0400 | [diff] [blame] | 298 | # Test weight focused on a single element of the population |
| 299 | self.assertEqual(choices('abcd', [1, 0, 0, 0]), ['a']) |
| 300 | self.assertEqual(choices('abcd', [0, 1, 0, 0]), ['b']) |
| 301 | self.assertEqual(choices('abcd', [0, 0, 1, 0]), ['c']) |
| 302 | self.assertEqual(choices('abcd', [0, 0, 0, 1]), ['d']) |
| 303 | |
| 304 | # Test consistency with random.choice() for empty population |
| 305 | with self.assertRaises(IndexError): |
| 306 | choices([], k=1) |
| 307 | with self.assertRaises(IndexError): |
| 308 | choices([], weights=[], k=1) |
| 309 | with self.assertRaises(IndexError): |
| 310 | choices([], cum_weights=[], k=5) |
| 311 | |
Raymond Hettinger | ddf7171 | 2018-06-27 01:08:31 -0700 | [diff] [blame] | 312 | def test_choices_subnormal(self): |
Min ho Kim | 96e12d5 | 2019-07-22 06:12:33 +1000 | [diff] [blame] | 313 | # Subnormal weights would occasionally trigger an IndexError |
Raymond Hettinger | ddf7171 | 2018-06-27 01:08:31 -0700 | [diff] [blame] | 314 | # in choices() when the value returned by random() was large |
| 315 | # enough to make `random() * total` round up to the total. |
| 316 | # See https://bugs.python.org/msg275594 for more detail. |
| 317 | choices = self.gen.choices |
| 318 | choices(population=[1, 2], weights=[1e-323, 1e-323], k=5000) |
| 319 | |
Raymond Hettinger | 041d8b4 | 2019-11-23 02:22:13 -0800 | [diff] [blame] | 320 | def test_choices_with_all_zero_weights(self): |
| 321 | # See issue #38881 |
| 322 | with self.assertRaises(ValueError): |
| 323 | self.gen.choices('AB', [0.0, 0.0]) |
| 324 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 325 | def test_gauss(self): |
| 326 | # Ensure that the seed() method initializes all the hidden state. In |
| 327 | # particular, through 2.2.1 it failed to reset a piece of state used |
| 328 | # by (and only by) the .gauss() method. |
| 329 | |
| 330 | for seed in 1, 12, 123, 1234, 12345, 123456, 654321: |
| 331 | self.gen.seed(seed) |
| 332 | x1 = self.gen.random() |
| 333 | y1 = self.gen.gauss(0, 1) |
| 334 | |
| 335 | self.gen.seed(seed) |
| 336 | x2 = self.gen.random() |
| 337 | y2 = self.gen.gauss(0, 1) |
| 338 | |
| 339 | self.assertEqual(x1, x2) |
| 340 | self.assertEqual(y1, y2) |
| 341 | |
Antoine Pitrou | 75a3378 | 2020-04-17 19:32:14 +0200 | [diff] [blame] | 342 | def test_getrandbits(self): |
| 343 | # Verify ranges |
| 344 | for k in range(1, 1000): |
| 345 | self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) |
| 346 | self.assertEqual(self.gen.getrandbits(0), 0) |
| 347 | |
| 348 | # Verify all bits active |
| 349 | getbits = self.gen.getrandbits |
| 350 | for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
| 351 | all_bits = 2**span-1 |
| 352 | cum = 0 |
| 353 | cpl_cum = 0 |
| 354 | for i in range(100): |
| 355 | v = getbits(span) |
| 356 | cum |= v |
| 357 | cpl_cum |= all_bits ^ v |
| 358 | self.assertEqual(cum, all_bits) |
| 359 | self.assertEqual(cpl_cum, all_bits) |
| 360 | |
| 361 | # Verify argument checking |
| 362 | self.assertRaises(TypeError, self.gen.getrandbits) |
| 363 | self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
| 364 | self.assertRaises(ValueError, self.gen.getrandbits, -1) |
| 365 | self.assertRaises(TypeError, self.gen.getrandbits, 10.1) |
| 366 | |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 367 | def test_pickling(self): |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 368 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 369 | state = pickle.dumps(self.gen, proto) |
| 370 | origseq = [self.gen.random() for i in range(10)] |
| 371 | newgen = pickle.loads(state) |
| 372 | restoredseq = [newgen.random() for i in range(10)] |
| 373 | self.assertEqual(origseq, restoredseq) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 374 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 375 | def test_bug_1727780(self): |
| 376 | # verify that version-2-pickles can be loaded |
| 377 | # fine, whether they are created on 32-bit or 64-bit |
| 378 | # platforms, and that version-3-pickles load fine. |
| 379 | files = [("randv2_32.pck", 780), |
| 380 | ("randv2_64.pck", 866), |
| 381 | ("randv3.pck", 343)] |
| 382 | for file, value in files: |
Serhiy Storchaka | 5b10b98 | 2019-03-05 10:06:26 +0200 | [diff] [blame] | 383 | with open(support.findfile(file),"rb") as f: |
| 384 | r = pickle.load(f) |
Raymond Hettinger | 0515661 | 2010-09-07 04:44:52 +0000 | [diff] [blame] | 385 | self.assertEqual(int(r.random()*1000), value) |
| 386 | |
| 387 | def test_bug_9025(self): |
| 388 | # Had problem with an uneven distribution in int(n*random()) |
| 389 | # Verify the fix by checking that distributions fall within expectations. |
| 390 | n = 100000 |
| 391 | randrange = self.gen.randrange |
| 392 | k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n)) |
| 393 | self.assertTrue(0.30 < k/n < .37, (k/n)) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 394 | |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 395 | def test_randbytes(self): |
| 396 | # Verify ranges |
| 397 | for n in range(1, 10): |
| 398 | data = self.gen.randbytes(n) |
| 399 | self.assertEqual(type(data), bytes) |
| 400 | self.assertEqual(len(data), n) |
| 401 | |
| 402 | self.assertEqual(self.gen.randbytes(0), b'') |
| 403 | |
| 404 | # Verify argument checking |
| 405 | self.assertRaises(TypeError, self.gen.randbytes) |
| 406 | self.assertRaises(TypeError, self.gen.randbytes, 1, 2) |
| 407 | self.assertRaises(ValueError, self.gen.randbytes, -1) |
| 408 | self.assertRaises(TypeError, self.gen.randbytes, 1.0) |
| 409 | |
| 410 | |
Ezio Melotti | 3e4a98b | 2013-04-19 05:45:27 +0300 | [diff] [blame] | 411 | try: |
| 412 | random.SystemRandom().random() |
| 413 | except NotImplementedError: |
| 414 | SystemRandom_available = False |
| 415 | else: |
| 416 | SystemRandom_available = True |
| 417 | |
| 418 | @unittest.skipUnless(SystemRandom_available, "random.SystemRandom not available") |
| 419 | class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase): |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 420 | gen = random.SystemRandom() |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 421 | |
| 422 | def test_autoseed(self): |
| 423 | # Doesn't need to do anything except not fail |
| 424 | self.gen.seed() |
| 425 | |
| 426 | def test_saverestore(self): |
| 427 | self.assertRaises(NotImplementedError, self.gen.getstate) |
| 428 | self.assertRaises(NotImplementedError, self.gen.setstate, None) |
| 429 | |
| 430 | def test_seedargs(self): |
| 431 | # Doesn't need to do anything except not fail |
| 432 | self.gen.seed(100) |
| 433 | |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 434 | def test_gauss(self): |
| 435 | self.gen.gauss_next = None |
| 436 | self.gen.seed(100) |
| 437 | self.assertEqual(self.gen.gauss_next, None) |
| 438 | |
| 439 | def test_pickling(self): |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 440 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 441 | self.assertRaises(NotImplementedError, pickle.dumps, self.gen, proto) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 442 | |
| 443 | def test_53_bits_per_float(self): |
| 444 | # This should pass whenever a C double has 53 bit precision. |
| 445 | span = 2 ** 53 |
| 446 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 447 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 448 | cum |= int(self.gen.random() * span) |
| 449 | self.assertEqual(cum, span-1) |
| 450 | |
| 451 | def test_bigrand(self): |
| 452 | # The randrange routine should build-up the required number of bits |
| 453 | # in stages so that all bit positions are active. |
| 454 | span = 2 ** 500 |
| 455 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 456 | for i in range(100): |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 457 | r = self.gen.randrange(span) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 458 | self.assertTrue(0 <= r < span) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 459 | cum |= r |
| 460 | self.assertEqual(cum, span-1) |
| 461 | |
| 462 | def test_bigrand_ranges(self): |
| 463 | for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
Zachary Ware | a6edea5 | 2013-11-26 14:50:10 -0600 | [diff] [blame] | 464 | start = self.gen.randrange(2 ** (i-2)) |
| 465 | stop = self.gen.randrange(2 ** i) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 466 | if stop <= start: |
Zachary Ware | a6edea5 | 2013-11-26 14:50:10 -0600 | [diff] [blame] | 467 | continue |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 468 | self.assertTrue(start <= self.gen.randrange(start, stop) < stop) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 469 | |
| 470 | def test_rangelimits(self): |
| 471 | for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
| 472 | self.assertEqual(set(range(start,stop)), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 473 | set([self.gen.randrange(start,stop) for i in range(100)])) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 474 | |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 475 | def test_randrange_nonunit_step(self): |
| 476 | rint = self.gen.randrange(0, 10, 2) |
| 477 | self.assertIn(rint, (0, 2, 4, 6, 8)) |
| 478 | rint = self.gen.randrange(0, 2, 2) |
| 479 | self.assertEqual(rint, 0) |
| 480 | |
| 481 | def test_randrange_errors(self): |
| 482 | raises = partial(self.assertRaises, ValueError, self.gen.randrange) |
| 483 | # Empty range |
| 484 | raises(3, 3) |
| 485 | raises(-721) |
| 486 | raises(0, 100, -12) |
| 487 | # Non-integer start/stop |
| 488 | raises(3.14159) |
| 489 | raises(0, 2.71828) |
| 490 | # Zero and non-integer step |
| 491 | raises(0, 42, 0) |
| 492 | raises(0, 42, 3.14159) |
| 493 | |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 494 | def test_randbelow_logic(self, _log=log, int=int): |
| 495 | # check bitcount transition points: 2**i and 2**(i+1)-1 |
| 496 | # show that: k = int(1.001 + _log(n, 2)) |
| 497 | # is equal to or one greater than the number of bits in n |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 498 | for i in range(1, 1000): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 499 | n = 1 << i # check an exact power of two |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 500 | numbits = i+1 |
| 501 | k = int(1.00001 + _log(n, 2)) |
| 502 | self.assertEqual(k, numbits) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 503 | self.assertEqual(n, 2**(k-1)) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 504 | |
| 505 | n += n - 1 # check 1 below the next power of two |
| 506 | k = int(1.00001 + _log(n, 2)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 507 | self.assertIn(k, [numbits, numbits+1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 508 | self.assertTrue(2**k > n > 2**(k-2)) |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 509 | |
| 510 | n -= n >> 15 # check a little farther below the next power of two |
| 511 | k = int(1.00001 + _log(n, 2)) |
| 512 | self.assertEqual(k, numbits) # note the stronger assertion |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 513 | self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 514 | |
| 515 | |
Ezio Melotti | 3e4a98b | 2013-04-19 05:45:27 +0300 | [diff] [blame] | 516 | class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase): |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 517 | gen = random.Random() |
| 518 | |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 519 | def test_guaranteed_stable(self): |
| 520 | # These sequences are guaranteed to stay the same across versions of python |
| 521 | self.gen.seed(3456147, version=1) |
| 522 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 523 | ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1', |
| 524 | '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1']) |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 525 | self.gen.seed("the quick brown fox", version=2) |
| 526 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
Raymond Hettinger | 3fcf002 | 2010-12-08 01:13:53 +0000 | [diff] [blame] | 527 | ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4', |
| 528 | '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1']) |
Raymond Hettinger | f763a72 | 2010-09-07 00:38:15 +0000 | [diff] [blame] | 529 | |
Raymond Hettinger | c7bab7c | 2016-08-31 15:01:08 -0700 | [diff] [blame] | 530 | def test_bug_27706(self): |
| 531 | # Verify that version 1 seeds are unaffected by hash randomization |
| 532 | |
| 533 | self.gen.seed('nofar', version=1) # hash('nofar') == 5990528763808513177 |
| 534 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 535 | ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5', |
| 536 | '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6']) |
| 537 | |
| 538 | self.gen.seed('rachel', version=1) # hash('rachel') == -9091735575445484789 |
| 539 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 540 | ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3', |
| 541 | '0x1.3052b9c072678p-2', '0x1.578f332106574p-3']) |
| 542 | |
| 543 | self.gen.seed('', version=1) # hash('') == 0 |
| 544 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 545 | ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1', |
| 546 | '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2']) |
| 547 | |
Oren Milman | d780b2d | 2017-09-28 10:50:01 +0300 | [diff] [blame] | 548 | def test_bug_31478(self): |
| 549 | # There shouldn't be an assertion failure in _random.Random.seed() in |
| 550 | # case the argument has a bad __abs__() method. |
| 551 | class BadInt(int): |
| 552 | def __abs__(self): |
| 553 | return None |
| 554 | try: |
| 555 | self.gen.seed(BadInt()) |
| 556 | except TypeError: |
| 557 | pass |
| 558 | |
Raymond Hettinger | 132a7d7 | 2017-09-17 09:04:30 -0700 | [diff] [blame] | 559 | def test_bug_31482(self): |
| 560 | # Verify that version 1 seeds are unaffected by hash randomization |
| 561 | # when the seeds are expressed as bytes rather than strings. |
| 562 | # The hash(b) values listed are the Python2.7 hash() values |
| 563 | # which were used for seeding. |
| 564 | |
| 565 | self.gen.seed(b'nofar', version=1) # hash('nofar') == 5990528763808513177 |
| 566 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 567 | ['0x1.8645314505ad7p-1', '0x1.afb1f82e40a40p-5', |
| 568 | '0x1.2a59d2285e971p-1', '0x1.56977142a7880p-6']) |
| 569 | |
| 570 | self.gen.seed(b'rachel', version=1) # hash('rachel') == -9091735575445484789 |
| 571 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 572 | ['0x1.0b294cc856fcdp-1', '0x1.2ad22d79e77b8p-3', |
| 573 | '0x1.3052b9c072678p-2', '0x1.578f332106574p-3']) |
| 574 | |
| 575 | self.gen.seed(b'', version=1) # hash('') == 0 |
| 576 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 577 | ['0x1.b0580f98a7dbep-1', '0x1.84129978f9c1ap-1', |
| 578 | '0x1.aeaa51052e978p-2', '0x1.092178fb945a6p-2']) |
| 579 | |
| 580 | b = b'\x00\x20\x40\x60\x80\xA0\xC0\xE0\xF0' |
| 581 | self.gen.seed(b, version=1) # hash(b) == 5015594239749365497 |
| 582 | self.assertEqual([self.gen.random().hex() for i in range(4)], |
| 583 | ['0x1.52c2fde444d23p-1', '0x1.875174f0daea4p-2', |
| 584 | '0x1.9e9b2c50e5cd2p-1', '0x1.fa57768bd321cp-2']) |
| 585 | |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 586 | def test_setstate_first_arg(self): |
| 587 | self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) |
| 588 | |
| 589 | def test_setstate_middle_arg(self): |
bladebryan | 9616a82 | 2017-04-21 23:10:46 -0700 | [diff] [blame] | 590 | start_state = self.gen.getstate() |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 591 | # Wrong type, s/b tuple |
| 592 | self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) |
| 593 | # Wrong length, s/b 625 |
| 594 | self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) |
| 595 | # Wrong type, s/b tuple of 625 ints |
| 596 | self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) |
| 597 | # Last element s/b an int also |
| 598 | self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) |
Serhiy Storchaka | 178f0b6 | 2015-07-24 09:02:53 +0300 | [diff] [blame] | 599 | # Last element s/b between 0 and 624 |
| 600 | with self.assertRaises((ValueError, OverflowError)): |
| 601 | self.gen.setstate((2, (1,)*624+(625,), None)) |
| 602 | with self.assertRaises((ValueError, OverflowError)): |
| 603 | self.gen.setstate((2, (1,)*624+(-1,), None)) |
bladebryan | 9616a82 | 2017-04-21 23:10:46 -0700 | [diff] [blame] | 604 | # Failed calls to setstate() should not have changed the state. |
| 605 | bits100 = self.gen.getrandbits(100) |
| 606 | self.gen.setstate(start_state) |
| 607 | self.assertEqual(self.gen.getrandbits(100), bits100) |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 608 | |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 609 | # Little trick to make "tuple(x % (2**32) for x in internalstate)" |
| 610 | # raise ValueError. I cannot think of a simple way to achieve this, so |
| 611 | # I am opting for using a generator as the middle argument of setstate |
| 612 | # which attempts to cast a NaN to integer. |
| 613 | state_values = self.gen.getstate()[1] |
| 614 | state_values = list(state_values) |
| 615 | state_values[-1] = float('nan') |
| 616 | state = (int(x) for x in state_values) |
| 617 | self.assertRaises(TypeError, self.gen.setstate, (2, state, None)) |
| 618 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 619 | def test_referenceImplementation(self): |
| 620 | # Compare the python implementation with results from the original |
| 621 | # code. Create 2000 53-bit precision random floats. Compare only |
| 622 | # the last ten entries to show that the independent implementations |
| 623 | # are tracking. Here is the main() function needed to create the |
| 624 | # list of expected random numbers: |
| 625 | # void main(void){ |
| 626 | # int i; |
| 627 | # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; |
| 628 | # init_by_array(init, length); |
| 629 | # for (i=0; i<2000; i++) { |
| 630 | # printf("%.15f ", genrand_res53()); |
| 631 | # if (i%5==4) printf("\n"); |
| 632 | # } |
| 633 | # } |
| 634 | expected = [0.45839803073713259, |
| 635 | 0.86057815201978782, |
| 636 | 0.92848331726782152, |
| 637 | 0.35932681119782461, |
| 638 | 0.081823493762449573, |
| 639 | 0.14332226470169329, |
| 640 | 0.084297823823520024, |
| 641 | 0.53814864671831453, |
| 642 | 0.089215024911993401, |
| 643 | 0.78486196105372907] |
| 644 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 645 | self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 646 | actual = self.randomlist(2000)[-10:] |
| 647 | for a, e in zip(actual, expected): |
| 648 | self.assertAlmostEqual(a,e,places=14) |
| 649 | |
| 650 | def test_strong_reference_implementation(self): |
| 651 | # Like test_referenceImplementation, but checks for exact bit-level |
| 652 | # equality. This should pass on any box where C double contains |
| 653 | # at least 53 bits of precision (the underlying algorithm suffers |
| 654 | # no rounding errors -- all results are exact). |
| 655 | from math import ldexp |
| 656 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 657 | expected = [0x0eab3258d2231f, |
| 658 | 0x1b89db315277a5, |
| 659 | 0x1db622a5518016, |
| 660 | 0x0b7f9af0d575bf, |
| 661 | 0x029e4c4db82240, |
| 662 | 0x04961892f5d673, |
| 663 | 0x02b291598e4589, |
| 664 | 0x11388382c15694, |
| 665 | 0x02dad977c9e1fe, |
| 666 | 0x191d96d4d334c6] |
| 667 | self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 668 | actual = self.randomlist(2000)[-10:] |
| 669 | for a, e in zip(actual, expected): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 670 | self.assertEqual(int(ldexp(a, 53)), e) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 671 | |
| 672 | def test_long_seed(self): |
| 673 | # This is most interesting to run in debug mode, just to make sure |
| 674 | # nothing blows up. Under the covers, a dynamically resized array |
| 675 | # is allocated, consuming space proportional to the number of bits |
| 676 | # in the seed. Unfortunately, that's a quadratic-time algorithm, |
| 677 | # so don't make this horribly big. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 678 | seed = (1 << (10000 * 8)) - 1 # about 10K bytes |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 679 | self.gen.seed(seed) |
| 680 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 681 | def test_53_bits_per_float(self): |
| 682 | # This should pass whenever a C double has 53 bit precision. |
| 683 | span = 2 ** 53 |
| 684 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 685 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 686 | cum |= int(self.gen.random() * span) |
| 687 | self.assertEqual(cum, span-1) |
| 688 | |
| 689 | def test_bigrand(self): |
| 690 | # The randrange routine should build-up the required number of bits |
| 691 | # in stages so that all bit positions are active. |
| 692 | span = 2 ** 500 |
| 693 | cum = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 694 | for i in range(100): |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 695 | r = self.gen.randrange(span) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 696 | self.assertTrue(0 <= r < span) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 697 | cum |= r |
| 698 | self.assertEqual(cum, span-1) |
| 699 | |
| 700 | def test_bigrand_ranges(self): |
| 701 | for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
Zachary Ware | a6edea5 | 2013-11-26 14:50:10 -0600 | [diff] [blame] | 702 | start = self.gen.randrange(2 ** (i-2)) |
| 703 | stop = self.gen.randrange(2 ** i) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 704 | if stop <= start: |
Zachary Ware | a6edea5 | 2013-11-26 14:50:10 -0600 | [diff] [blame] | 705 | continue |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 706 | self.assertTrue(start <= self.gen.randrange(start, stop) < stop) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 707 | |
| 708 | def test_rangelimits(self): |
| 709 | for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 710 | self.assertEqual(set(range(start,stop)), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 711 | set([self.gen.randrange(start,stop) for i in range(100)])) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | 75a3378 | 2020-04-17 19:32:14 +0200 | [diff] [blame] | 713 | def test_getrandbits(self): |
| 714 | super().test_getrandbits() |
| 715 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 716 | # Verify cross-platform repeatability |
| 717 | self.gen.seed(1234567) |
| 718 | self.assertEqual(self.gen.getrandbits(100), |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 719 | 97904845777343510404718956115) |
Raymond Hettinger | 5833587 | 2004-07-09 14:26:18 +0000 | [diff] [blame] | 720 | |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 721 | def test_randrange_uses_getrandbits(self): |
| 722 | # Verify use of getrandbits by randrange |
| 723 | # Use same seed as in the cross-platform repeatability test |
Antoine Pitrou | 75a3378 | 2020-04-17 19:32:14 +0200 | [diff] [blame] | 724 | # in test_getrandbits above. |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 725 | self.gen.seed(1234567) |
| 726 | # If randrange uses getrandbits, it should pick getrandbits(100) |
| 727 | # when called with a 100-bits stop argument. |
| 728 | self.assertEqual(self.gen.randrange(2**99), |
| 729 | 97904845777343510404718956115) |
| 730 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 731 | def test_randbelow_logic(self, _log=log, int=int): |
| 732 | # check bitcount transition points: 2**i and 2**(i+1)-1 |
| 733 | # show that: k = int(1.001 + _log(n, 2)) |
| 734 | # is equal to or one greater than the number of bits in n |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 735 | for i in range(1, 1000): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 736 | n = 1 << i # check an exact power of two |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 737 | numbits = i+1 |
| 738 | k = int(1.00001 + _log(n, 2)) |
| 739 | self.assertEqual(k, numbits) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 740 | self.assertEqual(n, 2**(k-1)) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 741 | |
| 742 | n += n - 1 # check 1 below the next power of two |
| 743 | k = int(1.00001 + _log(n, 2)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 744 | self.assertIn(k, [numbits, numbits+1]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 745 | self.assertTrue(2**k > n > 2**(k-2)) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 746 | |
| 747 | n -= n >> 15 # check a little farther below the next power of two |
| 748 | k = int(1.00001 + _log(n, 2)) |
| 749 | self.assertEqual(k, numbits) # note the stronger assertion |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 750 | self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 751 | |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 752 | def test_randbelow_without_getrandbits(self): |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 753 | # Random._randbelow() can only use random() when the built-in one |
| 754 | # has been overridden but no new getrandbits() method was supplied. |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 755 | maxsize = 1<<random.BPF |
| 756 | with warnings.catch_warnings(): |
| 757 | warnings.simplefilter("ignore", UserWarning) |
| 758 | # Population range too large (n >= maxsize) |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 759 | self.gen._randbelow_without_getrandbits( |
| 760 | maxsize+1, maxsize=maxsize |
| 761 | ) |
| 762 | self.gen._randbelow_without_getrandbits(5640, maxsize=maxsize) |
Raymond Hettinger | 4168f1e | 2020-05-01 10:34:19 -0700 | [diff] [blame] | 763 | # issue 33203: test that _randbelow returns zero on |
Wolfgang Maier | 091e95e | 2018-04-05 17:19:44 +0200 | [diff] [blame] | 764 | # n == 0 also in its getrandbits-independent branch. |
Raymond Hettinger | 4168f1e | 2020-05-01 10:34:19 -0700 | [diff] [blame] | 765 | x = self.gen._randbelow_without_getrandbits(0, maxsize=maxsize) |
| 766 | self.assertEqual(x, 0) |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 767 | |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 768 | # This might be going too far to test a single line, but because of our |
| 769 | # noble aim of achieving 100% test coverage we need to write a case in |
| 770 | # which the following line in Random._randbelow() gets executed: |
| 771 | # |
| 772 | # rem = maxsize % n |
| 773 | # limit = (maxsize - rem) / maxsize |
| 774 | # r = random() |
| 775 | # while r >= limit: |
| 776 | # r = random() # <== *This line* <==< |
| 777 | # |
| 778 | # Therefore, to guarantee that the while loop is executed at least |
| 779 | # once, we need to mock random() so that it returns a number greater |
| 780 | # than 'limit' the first time it gets called. |
| 781 | |
| 782 | n = 42 |
| 783 | epsilon = 0.01 |
| 784 | limit = (maxsize - (maxsize % n)) / maxsize |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 785 | with unittest.mock.patch.object(random.Random, 'random') as random_mock: |
| 786 | random_mock.side_effect = [limit + epsilon, limit - epsilon] |
| 787 | self.gen._randbelow_without_getrandbits(n, maxsize=maxsize) |
| 788 | self.assertEqual(random_mock.call_count, 2) |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 789 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 790 | def test_randrange_bug_1590891(self): |
| 791 | start = 1000000000000 |
| 792 | stop = -100000000000000000000 |
| 793 | step = -200 |
| 794 | x = self.gen.randrange(start, stop, step) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 795 | self.assertTrue(stop < x <= start) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 796 | self.assertEqual((x+stop)%step, 0) |
| 797 | |
Raymond Hettinger | 30d00e5 | 2016-10-29 16:55:36 -0700 | [diff] [blame] | 798 | def test_choices_algorithms(self): |
Raymond Hettinger | 24e4239 | 2016-11-13 00:42:56 -0500 | [diff] [blame] | 799 | # The various ways of specifying weights should produce the same results |
Raymond Hettinger | 30d00e5 | 2016-10-29 16:55:36 -0700 | [diff] [blame] | 800 | choices = self.gen.choices |
Raymond Hettinger | 6023d33 | 2016-11-21 15:32:08 -0800 | [diff] [blame] | 801 | n = 104729 |
Raymond Hettinger | 30d00e5 | 2016-10-29 16:55:36 -0700 | [diff] [blame] | 802 | |
| 803 | self.gen.seed(8675309) |
| 804 | a = self.gen.choices(range(n), k=10000) |
| 805 | |
| 806 | self.gen.seed(8675309) |
| 807 | b = self.gen.choices(range(n), [1]*n, k=10000) |
| 808 | self.assertEqual(a, b) |
| 809 | |
| 810 | self.gen.seed(8675309) |
| 811 | c = self.gen.choices(range(n), cum_weights=range(1, n+1), k=10000) |
| 812 | self.assertEqual(a, c) |
| 813 | |
penguindustin | 9646630 | 2019-05-06 14:57:17 -0400 | [diff] [blame] | 814 | # American Roulette |
Raymond Hettinger | 77d574d | 2016-10-29 17:42:36 -0700 | [diff] [blame] | 815 | population = ['Red', 'Black', 'Green'] |
| 816 | weights = [18, 18, 2] |
| 817 | cum_weights = [18, 36, 38] |
| 818 | expanded_population = ['Red'] * 18 + ['Black'] * 18 + ['Green'] * 2 |
| 819 | |
| 820 | self.gen.seed(9035768) |
| 821 | a = self.gen.choices(expanded_population, k=10000) |
| 822 | |
| 823 | self.gen.seed(9035768) |
| 824 | b = self.gen.choices(population, weights, k=10000) |
| 825 | self.assertEqual(a, b) |
| 826 | |
| 827 | self.gen.seed(9035768) |
| 828 | c = self.gen.choices(population, cum_weights=cum_weights, k=10000) |
| 829 | self.assertEqual(a, c) |
| 830 | |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 831 | def test_randbytes(self): |
| 832 | super().test_randbytes() |
| 833 | |
| 834 | # Mersenne Twister randbytes() is deterministic |
| 835 | # and does not depend on the endian and bitness. |
| 836 | seed = 8675309 |
Serhiy Storchaka | 223221b | 2020-04-17 23:51:28 +0300 | [diff] [blame] | 837 | expected = b'3\xa8\xf9f\xf4\xa4\xd06\x19\x8f\x9f\x82\x02oe\xf0' |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 838 | |
| 839 | self.gen.seed(seed) |
| 840 | self.assertEqual(self.gen.randbytes(16), expected) |
| 841 | |
| 842 | # randbytes(0) must not consume any entropy |
| 843 | self.gen.seed(seed) |
| 844 | self.assertEqual(self.gen.randbytes(0), b'') |
| 845 | self.assertEqual(self.gen.randbytes(16), expected) |
| 846 | |
| 847 | # Four randbytes(4) calls give the same output than randbytes(16) |
| 848 | self.gen.seed(seed) |
| 849 | self.assertEqual(b''.join([self.gen.randbytes(4) for _ in range(4)]), |
| 850 | expected) |
| 851 | |
Serhiy Storchaka | 223221b | 2020-04-17 23:51:28 +0300 | [diff] [blame] | 852 | # Each randbytes(1), randbytes(2) or randbytes(3) call consumes |
| 853 | # 4 bytes of entropy |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 854 | self.gen.seed(seed) |
Serhiy Storchaka | 223221b | 2020-04-17 23:51:28 +0300 | [diff] [blame] | 855 | expected1 = expected[3::4] |
| 856 | self.assertEqual(b''.join(self.gen.randbytes(1) for _ in range(4)), |
| 857 | expected1) |
| 858 | |
| 859 | self.gen.seed(seed) |
| 860 | expected2 = b''.join(expected[i + 2: i + 4] |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 861 | for i in range(0, len(expected), 4)) |
| 862 | self.assertEqual(b''.join(self.gen.randbytes(2) for _ in range(4)), |
| 863 | expected2) |
| 864 | |
| 865 | self.gen.seed(seed) |
Serhiy Storchaka | 223221b | 2020-04-17 23:51:28 +0300 | [diff] [blame] | 866 | expected3 = b''.join(expected[i + 1: i + 4] |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 867 | for i in range(0, len(expected), 4)) |
| 868 | self.assertEqual(b''.join(self.gen.randbytes(3) for _ in range(4)), |
| 869 | expected3) |
| 870 | |
Serhiy Storchaka | 223221b | 2020-04-17 23:51:28 +0300 | [diff] [blame] | 871 | def test_randbytes_getrandbits(self): |
| 872 | # There is a simple relation between randbytes() and getrandbits() |
| 873 | seed = 2849427419 |
| 874 | gen2 = random.Random() |
| 875 | self.gen.seed(seed) |
| 876 | gen2.seed(seed) |
| 877 | for n in range(9): |
| 878 | self.assertEqual(self.gen.randbytes(n), |
| 879 | gen2.getrandbits(n * 8).to_bytes(n, 'little')) |
| 880 | |
Victor Stinner | 9f5fe79 | 2020-04-17 19:05:35 +0200 | [diff] [blame] | 881 | |
Raymond Hettinger | 2d0c256 | 2009-02-19 09:53:18 +0000 | [diff] [blame] | 882 | def gamma(z, sqrt2pi=(2.0*pi)**0.5): |
| 883 | # Reflection to right half of complex plane |
| 884 | if z < 0.5: |
| 885 | return pi / sin(pi*z) / gamma(1.0-z) |
| 886 | # Lanczos approximation with g=7 |
| 887 | az = z + (7.0 - 0.5) |
| 888 | return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([ |
| 889 | 0.9999999999995183, |
| 890 | 676.5203681218835 / z, |
| 891 | -1259.139216722289 / (z+1.0), |
| 892 | 771.3234287757674 / (z+2.0), |
| 893 | -176.6150291498386 / (z+3.0), |
| 894 | 12.50734324009056 / (z+4.0), |
| 895 | -0.1385710331296526 / (z+5.0), |
| 896 | 0.9934937113930748e-05 / (z+6.0), |
| 897 | 0.1659470187408462e-06 / (z+7.0), |
| 898 | ]) |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 899 | |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 900 | class TestDistributions(unittest.TestCase): |
| 901 | def test_zeroinputs(self): |
| 902 | # Verify that distributions can handle a series of zero inputs' |
| 903 | g = random.Random() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 904 | x = [g.random() for i in range(50)] + [0.0]*5 |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 905 | g.random = x[:].pop; g.uniform(1,10) |
| 906 | g.random = x[:].pop; g.paretovariate(1.0) |
| 907 | g.random = x[:].pop; g.expovariate(1.0) |
| 908 | g.random = x[:].pop; g.weibullvariate(1.0, 1.0) |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 909 | g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0) |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 910 | g.random = x[:].pop; g.normalvariate(0.0, 1.0) |
| 911 | g.random = x[:].pop; g.gauss(0.0, 1.0) |
| 912 | g.random = x[:].pop; g.lognormvariate(0.0, 1.0) |
| 913 | g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) |
| 914 | g.random = x[:].pop; g.gammavariate(0.01, 1.0) |
| 915 | g.random = x[:].pop; g.gammavariate(1.0, 1.0) |
| 916 | g.random = x[:].pop; g.gammavariate(200.0, 1.0) |
| 917 | g.random = x[:].pop; g.betavariate(3.0, 3.0) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 918 | g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) |
Raymond Hettinger | 15ec373 | 2003-01-05 01:08:34 +0000 | [diff] [blame] | 919 | |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 920 | def test_avg_std(self): |
| 921 | # Use integration to test distribution average and standard deviation. |
| 922 | # Only works for distributions which do not consume variates in pairs |
| 923 | g = random.Random() |
| 924 | N = 5000 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 925 | x = [i/float(N) for i in range(1,N)] |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 926 | for variate, args, mu, sigmasqrd in [ |
| 927 | (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 928 | (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0), |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 929 | (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 930 | (g.vonmisesvariate, (1.23, 0), pi, pi**2/3), |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 931 | (g.paretovariate, (5.0,), 5.0/(5.0-1), |
| 932 | 5.0/((5.0-1)**2*(5.0-2))), |
| 933 | (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), |
| 934 | gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: |
| 935 | g.random = x[:].pop |
| 936 | y = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 937 | for i in range(len(x)): |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 938 | try: |
| 939 | y.append(variate(*args)) |
| 940 | except IndexError: |
| 941 | pass |
| 942 | s1 = s2 = 0 |
| 943 | for e in y: |
| 944 | s1 += e |
| 945 | s2 += (e - mu) ** 2 |
| 946 | N = len(y) |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 947 | self.assertAlmostEqual(s1/N, mu, places=2, |
| 948 | msg='%s%r' % (variate.__name__, args)) |
| 949 | self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2, |
| 950 | msg='%s%r' % (variate.__name__, args)) |
| 951 | |
| 952 | def test_constant(self): |
| 953 | g = random.Random() |
| 954 | N = 100 |
| 955 | for variate, args, expected in [ |
| 956 | (g.uniform, (10.0, 10.0), 10.0), |
| 957 | (g.triangular, (10.0, 10.0), 10.0), |
Raymond Hettinger | 978c6ab | 2014-05-25 17:25:27 -0700 | [diff] [blame] | 958 | (g.triangular, (10.0, 10.0, 10.0), 10.0), |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 959 | (g.expovariate, (float('inf'),), 0.0), |
| 960 | (g.vonmisesvariate, (3.0, float('inf')), 3.0), |
| 961 | (g.gauss, (10.0, 0.0), 10.0), |
| 962 | (g.lognormvariate, (0.0, 0.0), 1.0), |
| 963 | (g.lognormvariate, (-float('inf'), 0.0), 0.0), |
| 964 | (g.normalvariate, (10.0, 0.0), 10.0), |
| 965 | (g.paretovariate, (float('inf'),), 1.0), |
| 966 | (g.weibullvariate, (10.0, float('inf')), 10.0), |
| 967 | (g.weibullvariate, (0.0, 10.0), 0.0), |
| 968 | ]: |
| 969 | for i in range(N): |
| 970 | self.assertEqual(variate(*args), expected) |
Raymond Hettinger | 3dd990c | 2003-01-05 09:20:06 +0000 | [diff] [blame] | 971 | |
Mark Dickinson | be5f919 | 2013-02-10 14:16:10 +0000 | [diff] [blame] | 972 | def test_von_mises_range(self): |
| 973 | # Issue 17149: von mises variates were not consistently in the |
| 974 | # range [0, 2*PI]. |
| 975 | g = random.Random() |
| 976 | N = 100 |
| 977 | for mu in 0.0, 0.1, 3.1, 6.2: |
| 978 | for kappa in 0.0, 2.3, 500.0: |
| 979 | for _ in range(N): |
| 980 | sample = g.vonmisesvariate(mu, kappa) |
| 981 | self.assertTrue( |
| 982 | 0 <= sample <= random.TWOPI, |
| 983 | msg=("vonmisesvariate({}, {}) produced a result {} out" |
| 984 | " of range [0, 2*pi]").format(mu, kappa, sample)) |
| 985 | |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 986 | def test_von_mises_large_kappa(self): |
| 987 | # Issue #17141: vonmisesvariate() was hang for large kappas |
| 988 | random.vonmisesvariate(0, 1e15) |
| 989 | random.vonmisesvariate(0, 1e100) |
| 990 | |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 991 | def test_gammavariate_errors(self): |
| 992 | # Both alpha and beta must be > 0.0 |
| 993 | self.assertRaises(ValueError, random.gammavariate, -1, 3) |
| 994 | self.assertRaises(ValueError, random.gammavariate, 0, 2) |
| 995 | self.assertRaises(ValueError, random.gammavariate, 2, 0) |
| 996 | self.assertRaises(ValueError, random.gammavariate, 1, -3) |
| 997 | |
leodema | 63d1522 | 2018-12-24 07:54:25 +0100 | [diff] [blame] | 998 | # There are three different possibilities in the current implementation |
| 999 | # of random.gammavariate(), depending on the value of 'alpha'. What we |
| 1000 | # are going to do here is to fix the values returned by random() to |
| 1001 | # generate test cases that provide 100% line coverage of the method. |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 1002 | @unittest.mock.patch('random.Random.random') |
leodema | 63d1522 | 2018-12-24 07:54:25 +0100 | [diff] [blame] | 1003 | def test_gammavariate_alpha_greater_one(self, random_mock): |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 1004 | |
leodema | 63d1522 | 2018-12-24 07:54:25 +0100 | [diff] [blame] | 1005 | # #1: alpha > 1.0. |
| 1006 | # We want the first random number to be outside the |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 1007 | # [1e-7, .9999999] range, so that the continue statement executes |
| 1008 | # once. The values of u1 and u2 will be 0.5 and 0.3, respectively. |
| 1009 | random_mock.side_effect = [1e-8, 0.5, 0.3] |
| 1010 | returned_value = random.gammavariate(1.1, 2.3) |
| 1011 | self.assertAlmostEqual(returned_value, 2.53) |
| 1012 | |
leodema | 63d1522 | 2018-12-24 07:54:25 +0100 | [diff] [blame] | 1013 | @unittest.mock.patch('random.Random.random') |
| 1014 | def test_gammavariate_alpha_equal_one(self, random_mock): |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 1015 | |
leodema | 63d1522 | 2018-12-24 07:54:25 +0100 | [diff] [blame] | 1016 | # #2.a: alpha == 1. |
| 1017 | # The execution body of the while loop executes once. |
| 1018 | # Then random.random() returns 0.45, |
| 1019 | # which causes while to stop looping and the algorithm to terminate. |
| 1020 | random_mock.side_effect = [0.45] |
| 1021 | returned_value = random.gammavariate(1.0, 3.14) |
| 1022 | self.assertAlmostEqual(returned_value, 1.877208182372648) |
| 1023 | |
| 1024 | @unittest.mock.patch('random.Random.random') |
| 1025 | def test_gammavariate_alpha_equal_one_equals_expovariate(self, random_mock): |
| 1026 | |
| 1027 | # #2.b: alpha == 1. |
| 1028 | # It must be equivalent of calling expovariate(1.0 / beta). |
| 1029 | beta = 3.14 |
| 1030 | random_mock.side_effect = [1e-8, 1e-8] |
| 1031 | gammavariate_returned_value = random.gammavariate(1.0, beta) |
| 1032 | expovariate_returned_value = random.expovariate(1.0 / beta) |
| 1033 | self.assertAlmostEqual(gammavariate_returned_value, expovariate_returned_value) |
| 1034 | |
| 1035 | @unittest.mock.patch('random.Random.random') |
| 1036 | def test_gammavariate_alpha_between_zero_and_one(self, random_mock): |
| 1037 | |
| 1038 | # #3: 0 < alpha < 1. |
| 1039 | # This is the most complex region of code to cover, |
R David Murray | e3e1c17 | 2013-04-02 12:47:23 -0400 | [diff] [blame] | 1040 | # as there are multiple if-else statements. Let's take a look at the |
| 1041 | # source code, and determine the values that we need accordingly: |
| 1042 | # |
| 1043 | # while 1: |
| 1044 | # u = random() |
| 1045 | # b = (_e + alpha)/_e |
| 1046 | # p = b*u |
| 1047 | # if p <= 1.0: # <=== (A) |
| 1048 | # x = p ** (1.0/alpha) |
| 1049 | # else: # <=== (B) |
| 1050 | # x = -_log((b-p)/alpha) |
| 1051 | # u1 = random() |
| 1052 | # if p > 1.0: # <=== (C) |
| 1053 | # if u1 <= x ** (alpha - 1.0): # <=== (D) |
| 1054 | # break |
| 1055 | # elif u1 <= _exp(-x): # <=== (E) |
| 1056 | # break |
| 1057 | # return x * beta |
| 1058 | # |
| 1059 | # First, we want (A) to be True. For that we need that: |
| 1060 | # b*random() <= 1.0 |
| 1061 | # r1 = random() <= 1.0 / b |
| 1062 | # |
| 1063 | # We now get to the second if-else branch, and here, since p <= 1.0, |
| 1064 | # (C) is False and we take the elif branch, (E). For it to be True, |
| 1065 | # so that the break is executed, we need that: |
| 1066 | # r2 = random() <= _exp(-x) |
| 1067 | # r2 <= _exp(-(p ** (1.0/alpha))) |
| 1068 | # r2 <= _exp(-((b*r1) ** (1.0/alpha))) |
| 1069 | |
| 1070 | _e = random._e |
| 1071 | _exp = random._exp |
| 1072 | _log = random._log |
| 1073 | alpha = 0.35 |
| 1074 | beta = 1.45 |
| 1075 | b = (_e + alpha)/_e |
| 1076 | epsilon = 0.01 |
| 1077 | |
| 1078 | r1 = 0.8859296441566 # 1.0 / b |
| 1079 | r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha))) |
| 1080 | |
| 1081 | # These four "random" values result in the following trace: |
| 1082 | # (A) True, (E) False --> [next iteration of while] |
| 1083 | # (A) True, (E) True --> [while loop breaks] |
| 1084 | random_mock.side_effect = [r1, r2 + epsilon, r1, r2] |
| 1085 | returned_value = random.gammavariate(alpha, beta) |
| 1086 | self.assertAlmostEqual(returned_value, 1.4499999999997544) |
| 1087 | |
| 1088 | # Let's now make (A) be False. If this is the case, when we get to the |
| 1089 | # second if-else 'p' is greater than 1, so (C) evaluates to True. We |
| 1090 | # now encounter a second if statement, (D), which in order to execute |
| 1091 | # must satisfy the following condition: |
| 1092 | # r2 <= x ** (alpha - 1.0) |
| 1093 | # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0) |
| 1094 | # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0) |
| 1095 | r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False |
| 1096 | r2 = 0.9445400408898141 |
| 1097 | |
| 1098 | # And these four values result in the following trace: |
| 1099 | # (B) and (C) True, (D) False --> [next iteration of while] |
| 1100 | # (B) and (C) True, (D) True [while loop breaks] |
| 1101 | random_mock.side_effect = [r1, r2 + epsilon, r1, r2] |
| 1102 | returned_value = random.gammavariate(alpha, beta) |
| 1103 | self.assertAlmostEqual(returned_value, 1.5830349561760781) |
| 1104 | |
| 1105 | @unittest.mock.patch('random.Random.gammavariate') |
| 1106 | def test_betavariate_return_zero(self, gammavariate_mock): |
| 1107 | # betavariate() returns zero when the Gamma distribution |
| 1108 | # that it uses internally returns this same value. |
| 1109 | gammavariate_mock.return_value = 0.0 |
| 1110 | self.assertEqual(0.0, random.betavariate(2.71828, 3.14159)) |
Serhiy Storchaka | 6c22b1d | 2013-02-10 19:28:56 +0200 | [diff] [blame] | 1111 | |
Serhiy Storchaka | ec1622d | 2018-05-08 15:45:15 +0300 | [diff] [blame] | 1112 | |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 1113 | class TestRandomSubclassing(unittest.TestCase): |
| 1114 | def test_random_subclass_with_kwargs(self): |
| 1115 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 1116 | class Subclass(random.Random): |
| 1117 | def __init__(self, newarg=None): |
| 1118 | random.Random.__init__(self) |
| 1119 | Subclass(newarg=1) |
| 1120 | |
| 1121 | def test_subclasses_overriding_methods(self): |
| 1122 | # Subclasses with an overridden random, but only the original |
| 1123 | # getrandbits method should not rely on getrandbits in for randrange, |
| 1124 | # but should use a getrandbits-independent implementation instead. |
| 1125 | |
| 1126 | # subclass providing its own random **and** getrandbits methods |
| 1127 | # like random.SystemRandom does => keep relying on getrandbits for |
| 1128 | # randrange |
| 1129 | class SubClass1(random.Random): |
| 1130 | def random(self): |
Serhiy Storchaka | ec1622d | 2018-05-08 15:45:15 +0300 | [diff] [blame] | 1131 | called.add('SubClass1.random') |
| 1132 | return random.Random.random(self) |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 1133 | |
| 1134 | def getrandbits(self, n): |
Serhiy Storchaka | ec1622d | 2018-05-08 15:45:15 +0300 | [diff] [blame] | 1135 | called.add('SubClass1.getrandbits') |
| 1136 | return random.Random.getrandbits(self, n) |
| 1137 | called = set() |
| 1138 | SubClass1().randrange(42) |
| 1139 | self.assertEqual(called, {'SubClass1.getrandbits'}) |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 1140 | |
| 1141 | # subclass providing only random => can only use random for randrange |
| 1142 | class SubClass2(random.Random): |
| 1143 | def random(self): |
Serhiy Storchaka | ec1622d | 2018-05-08 15:45:15 +0300 | [diff] [blame] | 1144 | called.add('SubClass2.random') |
| 1145 | return random.Random.random(self) |
| 1146 | called = set() |
| 1147 | SubClass2().randrange(42) |
| 1148 | self.assertEqual(called, {'SubClass2.random'}) |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 1149 | |
| 1150 | # subclass defining getrandbits to complement its inherited random |
| 1151 | # => can now rely on getrandbits for randrange again |
| 1152 | class SubClass3(SubClass2): |
| 1153 | def getrandbits(self, n): |
Serhiy Storchaka | ec1622d | 2018-05-08 15:45:15 +0300 | [diff] [blame] | 1154 | called.add('SubClass3.getrandbits') |
| 1155 | return random.Random.getrandbits(self, n) |
| 1156 | called = set() |
| 1157 | SubClass3().randrange(42) |
| 1158 | self.assertEqual(called, {'SubClass3.getrandbits'}) |
| 1159 | |
| 1160 | # subclass providing only random and inherited getrandbits |
| 1161 | # => random takes precedence |
| 1162 | class SubClass4(SubClass3): |
| 1163 | def random(self): |
| 1164 | called.add('SubClass4.random') |
| 1165 | return random.Random.random(self) |
| 1166 | called = set() |
| 1167 | SubClass4().randrange(42) |
| 1168 | self.assertEqual(called, {'SubClass4.random'}) |
| 1169 | |
| 1170 | # Following subclasses don't define random or getrandbits directly, |
| 1171 | # but inherit them from classes which are not subclasses of Random |
| 1172 | class Mixin1: |
| 1173 | def random(self): |
| 1174 | called.add('Mixin1.random') |
| 1175 | return random.Random.random(self) |
| 1176 | class Mixin2: |
| 1177 | def getrandbits(self, n): |
| 1178 | called.add('Mixin2.getrandbits') |
| 1179 | return random.Random.getrandbits(self, n) |
| 1180 | |
| 1181 | class SubClass5(Mixin1, random.Random): |
| 1182 | pass |
| 1183 | called = set() |
| 1184 | SubClass5().randrange(42) |
| 1185 | self.assertEqual(called, {'Mixin1.random'}) |
| 1186 | |
| 1187 | class SubClass6(Mixin2, random.Random): |
| 1188 | pass |
| 1189 | called = set() |
| 1190 | SubClass6().randrange(42) |
| 1191 | self.assertEqual(called, {'Mixin2.getrandbits'}) |
| 1192 | |
| 1193 | class SubClass7(Mixin1, Mixin2, random.Random): |
| 1194 | pass |
| 1195 | called = set() |
| 1196 | SubClass7().randrange(42) |
| 1197 | self.assertEqual(called, {'Mixin1.random'}) |
| 1198 | |
| 1199 | class SubClass8(Mixin2, Mixin1, random.Random): |
| 1200 | pass |
| 1201 | called = set() |
| 1202 | SubClass8().randrange(42) |
| 1203 | self.assertEqual(called, {'Mixin2.getrandbits'}) |
| 1204 | |
Wolfgang Maier | ba3a87a | 2018-04-17 17:16:17 +0200 | [diff] [blame] | 1205 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1206 | class TestModule(unittest.TestCase): |
| 1207 | def testMagicConstants(self): |
| 1208 | self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) |
| 1209 | self.assertAlmostEqual(random.TWOPI, 6.28318530718) |
| 1210 | self.assertAlmostEqual(random.LOG4, 1.38629436111989) |
| 1211 | self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) |
| 1212 | |
| 1213 | def test__all__(self): |
| 1214 | # tests validity but not completeness of the __all__ list |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1215 | self.assertTrue(set(random.__all__) <= set(dir(random))) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 1217 | @unittest.skipUnless(hasattr(os, "fork"), "fork() required") |
| 1218 | def test_after_fork(self): |
| 1219 | # Test the global Random instance gets reseeded in child |
| 1220 | r, w = os.pipe() |
Victor Stinner | da5e930 | 2017-08-09 17:59:05 +0200 | [diff] [blame] | 1221 | pid = os.fork() |
| 1222 | if pid == 0: |
| 1223 | # child process |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 1224 | try: |
| 1225 | val = random.getrandbits(128) |
| 1226 | with open(w, "w") as f: |
| 1227 | f.write(str(val)) |
| 1228 | finally: |
| 1229 | os._exit(0) |
| 1230 | else: |
Victor Stinner | da5e930 | 2017-08-09 17:59:05 +0200 | [diff] [blame] | 1231 | # parent process |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 1232 | os.close(w) |
| 1233 | val = random.getrandbits(128) |
| 1234 | with open(r, "r") as f: |
| 1235 | child_val = eval(f.read()) |
| 1236 | self.assertNotEqual(val, child_val) |
| 1237 | |
Victor Stinner | 278c1e1 | 2020-03-31 20:08:12 +0200 | [diff] [blame] | 1238 | support.wait_process(pid, exitcode=0) |
Victor Stinner | da5e930 | 2017-08-09 17:59:05 +0200 | [diff] [blame] | 1239 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1240 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 1241 | if __name__ == "__main__": |
Ezio Melotti | 3e4a98b | 2013-04-19 05:45:27 +0300 | [diff] [blame] | 1242 | unittest.main() |