Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_random.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 28ec217ce510 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
58415
diff
changeset
|
1 #!/usr/bin/env python3 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
2 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
3 import unittest |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
4 import unittest.mock |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
5 import random |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
6 import time |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
7 import pickle |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
8 import warnings |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
9 from functools import partial |
|
59615
b93cfafcdcad
Merged revisions 78093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
59522
diff
changeset
|
10 from math import log, exp, pi, fsum, sin |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46034
diff
changeset
|
11 from test import support |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
12 |
|
83447
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
13 class TestBasicOps: |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
14 # Superclass with tests common to all generators. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
15 # Subclasses must arrange for self.gen to retrieve the Random instance |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
16 # to be tested. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
17 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
18 def randomlist(self, n): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
19 """Helper function to make a list of random numbers""" |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
20 return [self.gen.random() for i in range(n)] |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
21 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
22 def test_autoseed(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
23 self.gen.seed() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
24 state1 = self.gen.getstate() |
|
30114
9f6e2dee1892
SF bug #778964: bad seed in python 2.3 random
Raymond Hettinger <python@rcn.com>
parents:
29586
diff
changeset
|
25 time.sleep(0.1) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
26 self.gen.seed() # diffent seeds at different times |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
27 state2 = self.gen.getstate() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
28 self.assertNotEqual(state1, state2) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
29 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
30 def test_saverestore(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
31 N = 1000 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
32 self.gen.seed() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
33 state = self.gen.getstate() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
34 randseq = self.randomlist(N) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
35 self.gen.setstate(state) # should regenerate the same sequence |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
36 self.assertEqual(randseq, self.randomlist(N)) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
37 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
38 def test_seedargs(self): |
|
77688
62d9f1285f51
In random's test_seedargs: Make sure to include at least one seed object with a negative hash.
Mark Dickinson <mdickinson@enthought.com>
parents:
66786
diff
changeset
|
39 # Seed value with a negative hash. |
|
62d9f1285f51
In random's test_seedargs: Make sure to include at least one seed object with a negative hash.
Mark Dickinson <mdickinson@enthought.com>
parents:
66786
diff
changeset
|
40 class MySeed(object): |
|
62d9f1285f51
In random's test_seedargs: Make sure to include at least one seed object with a negative hash.
Mark Dickinson <mdickinson@enthought.com>
parents:
66786
diff
changeset
|
41 def __hash__(self): |
|
62d9f1285f51
In random's test_seedargs: Make sure to include at least one seed object with a negative hash.
Mark Dickinson <mdickinson@enthought.com>
parents:
66786
diff
changeset
|
42 return -1729 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
43 for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20), |
|
77688
62d9f1285f51
In random's test_seedargs: Make sure to include at least one seed object with a negative hash.
Mark Dickinson <mdickinson@enthought.com>
parents:
66786
diff
changeset
|
44 3.14, 1+2j, 'a', tuple('abc'), MySeed()]: |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
45 self.gen.seed(arg) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
46 for arg in [list(range(3)), dict(one=1)]: |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
47 self.assertRaises(TypeError, self.gen.seed, arg) |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
48 self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4) |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
49 self.assertRaises(TypeError, type(self.gen), []) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
50 |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
51 @unittest.mock.patch('random._urandom') # os.urandom |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
52 def test_seed_when_randomness_source_not_found(self, urandom_mock): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
53 # Random.seed() uses time.time() when an operating system specific |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
54 # randomness source is not found. To test this on machines were it |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
55 # exists, run the above test, test_seedargs(), again after mocking |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
56 # os.urandom() so that it raises the exception expected when the |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
57 # randomness source is not available. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
58 urandom_mock.side_effect = NotImplementedError |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
59 self.test_seedargs() |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
60 |
|
80230
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
61 def test_shuffle(self): |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
62 shuffle = self.gen.shuffle |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
63 lst = [] |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
64 shuffle(lst) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
65 self.assertEqual(lst, []) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
66 lst = [37] |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
67 shuffle(lst) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
68 self.assertEqual(lst, [37]) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
69 seqs = [list(range(n)) for n in range(10)] |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
70 shuffled_seqs = [list(range(n)) for n in range(10)] |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
71 for shuffled_seq in shuffled_seqs: |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
72 shuffle(shuffled_seq) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
73 for (seq, shuffled_seq) in zip(seqs, shuffled_seqs): |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
74 self.assertEqual(len(seq), len(shuffled_seq)) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
75 self.assertEqual(set(seq), set(shuffled_seq)) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
76 # The above tests all would pass if the shuffle was a |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
77 # no-op. The following non-deterministic test covers that. It |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
78 # asserts that the shuffled sequence of 1000 distinct elements |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
79 # must be different from the original one. Although there is |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
80 # mathematically a non-zero probability that this could |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
81 # actually happen in a genuinely random shuffle, it is |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
82 # completely negligible, given that the number of possible |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
83 # permutations of 1000 objects is 1000! (factorial of 1000), |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
84 # which is considerably larger than the number of atoms in the |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
85 # universe... |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
86 lst = list(range(1000)) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
87 shuffled_lst = list(range(1000)) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
88 shuffle(shuffled_lst) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
89 self.assertTrue(lst != shuffled_lst) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
90 shuffle(lst) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
91 self.assertTrue(lst != shuffled_lst) |
|
58776cc74e89
Issue #15837: add some tests for random.shuffle().
Antoine Pitrou <solipsis@pitrou.net>
parents:
77688
diff
changeset
|
92 |
|
64517
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
93 def test_choice(self): |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
94 choice = self.gen.choice |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
95 with self.assertRaises(IndexError): |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
96 choice([]) |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
97 self.assertEqual(choice([50]), 50) |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
98 self.assertIn(choice([25, 75]), [25, 75]) |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
99 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
100 def test_sample(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
101 # For the entire allowable range of 0 <= k <= N, validate that |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
102 # the sample is of the correct length and contains only unique items |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
103 N = 100 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
104 population = range(N) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
105 for k in range(N+1): |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
106 s = self.gen.sample(population, k) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
107 self.assertEqual(len(s), k) |
|
30929
54ff68e973ee
* Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
30649
diff
changeset
|
108 uniq = set(s) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
109 self.assertEqual(len(uniq), k) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
110 self.assertTrue(uniq <= set(population)) |
|
27081
3199e974f55a
Test an edge case for sample().
Raymond Hettinger <python@rcn.com>
parents:
26829
diff
changeset
|
111 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
112 # Exception raised if size of sample exceeds that of population |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
113 self.assertRaises(ValueError, self.gen.sample, population, N+1) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
114 |
|
27279
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
115 def test_sample_distribution(self): |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
116 # For the entire allowable range of 0 <= k <= N, validate that |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
117 # sample generates all possible permutations |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
118 n = 5 |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
119 pop = range(n) |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
120 trials = 10000 # large num prevents false negatives without slowing normal case |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
121 def factorial(n): |
|
39627
e86c0b29b4a2
Kill reduce(). A coproduction of John Reese, Jacques Frechet, and Alex M.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
122 if n == 0: |
|
e86c0b29b4a2
Kill reduce(). A coproduction of John Reese, Jacques Frechet, and Alex M.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
123 return 1 |
|
e86c0b29b4a2
Kill reduce(). A coproduction of John Reese, Jacques Frechet, and Alex M.
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
124 return n * factorial(n - 1) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
125 for k in range(n): |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33481
diff
changeset
|
126 expected = factorial(n) // factorial(n-k) |
|
27279
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
127 perms = {} |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
128 for i in range(trials): |
|
27279
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
129 perms[tuple(self.gen.sample(pop, k))] = None |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
130 if len(perms) == expected: |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
131 break |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
132 else: |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
133 self.fail() |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
134 |
|
30346
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
135 def test_sample_inputs(self): |
|
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
136 # SF bug #801342 -- population can be any iterable defining __len__() |
|
30929
54ff68e973ee
* Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
30649
diff
changeset
|
137 self.gen.sample(set(range(20)), 2) |
|
30346
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
138 self.gen.sample(range(20), 2) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
139 self.gen.sample(range(20), 2) |
|
30346
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
140 self.gen.sample(str('abcdefghijklmnopqrst'), 2) |
|
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
141 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) |
|
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
142 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
33597
diff
changeset
|
143 def test_sample_on_dicts(self): |
|
44665
91928d99ebb0
Take Tim's advice and have random.sample() support only sequences and sets.
Raymond Hettinger <python@rcn.com>
parents:
44662
diff
changeset
|
144 self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
33597
diff
changeset
|
145 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
146 def test_gauss(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
147 # Ensure that the seed() method initializes all the hidden state. In |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
148 # particular, through 2.2.1 it failed to reset a piece of state used |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
149 # by (and only by) the .gauss() method. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
150 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
151 for seed in 1, 12, 123, 1234, 12345, 123456, 654321: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
152 self.gen.seed(seed) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
153 x1 = self.gen.random() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
154 y1 = self.gen.gauss(0, 1) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
155 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
156 self.gen.seed(seed) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
157 x2 = self.gen.random() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
158 y2 = self.gen.gauss(0, 1) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
159 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
160 self.assertEqual(x1, x2) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
161 self.assertEqual(y1, y2) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
162 |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
163 def test_pickling(self): |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
164 state = pickle.dumps(self.gen) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
165 origseq = [self.gen.random() for i in range(10)] |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
166 newgen = pickle.loads(state) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
167 restoredseq = [newgen.random() for i in range(10)] |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
168 self.assertEqual(origseq, restoredseq) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
169 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
170 def test_bug_1727780(self): |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
171 # verify that version-2-pickles can be loaded |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
172 # fine, whether they are created on 32-bit or 64-bit |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
173 # platforms, and that version-3-pickles load fine. |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
174 files = [("randv2_32.pck", 780), |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
175 ("randv2_64.pck", 866), |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
176 ("randv3.pck", 343)] |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
177 for file, value in files: |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46034
diff
changeset
|
178 f = open(support.findfile(file),"rb") |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
179 r = pickle.load(f) |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
180 f.close() |
|
64512
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
181 self.assertEqual(int(r.random()*1000), value) |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
182 |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
183 def test_bug_9025(self): |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
184 # Had problem with an uneven distribution in int(n*random()) |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
185 # Verify the fix by checking that distributions fall within expectations. |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
186 n = 100000 |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
187 randrange = self.gen.randrange |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
188 k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n)) |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
189 self.assertTrue(0.30 < k/n < .37, (k/n)) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
190 |
|
83447
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
191 try: |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
192 random.SystemRandom().random() |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
193 except NotImplementedError: |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
194 SystemRandom_available = False |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
195 else: |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
196 SystemRandom_available = True |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
197 |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
198 @unittest.skipUnless(SystemRandom_available, "random.SystemRandom not available") |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
199 class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase): |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
200 gen = random.SystemRandom() |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
201 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
202 def test_autoseed(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
203 # Doesn't need to do anything except not fail |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
204 self.gen.seed() |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
205 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
206 def test_saverestore(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
207 self.assertRaises(NotImplementedError, self.gen.getstate) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
208 self.assertRaises(NotImplementedError, self.gen.setstate, None) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
209 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
210 def test_seedargs(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
211 # Doesn't need to do anything except not fail |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
212 self.gen.seed(100) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
213 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
214 def test_gauss(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
215 self.gen.gauss_next = None |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
216 self.gen.seed(100) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
217 self.assertEqual(self.gen.gauss_next, None) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
218 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
219 def test_pickling(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
220 self.assertRaises(NotImplementedError, pickle.dumps, self.gen) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
221 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
222 def test_53_bits_per_float(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
223 # This should pass whenever a C double has 53 bit precision. |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
224 span = 2 ** 53 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
225 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
226 for i in range(100): |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
227 cum |= int(self.gen.random() * span) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
228 self.assertEqual(cum, span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
229 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
230 def test_bigrand(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
231 # The randrange routine should build-up the required number of bits |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
232 # in stages so that all bit positions are active. |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
233 span = 2 ** 500 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
234 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
235 for i in range(100): |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
236 r = self.gen.randrange(span) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
237 self.assertTrue(0 <= r < span) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
238 cum |= r |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
239 self.assertEqual(cum, span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
240 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
241 def test_bigrand_ranges(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
242 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
|
87597
c65882d79c5f
Issue #19588: Fixed tests in test_random that were silently skipped most
Zachary Ware <zachary.ware@gmail.com>
parents:
83447
diff
changeset
|
243 start = self.gen.randrange(2 ** (i-2)) |
|
c65882d79c5f
Issue #19588: Fixed tests in test_random that were silently skipped most
Zachary Ware <zachary.ware@gmail.com>
parents:
83447
diff
changeset
|
244 stop = self.gen.randrange(2 ** i) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
245 if stop <= start: |
|
87597
c65882d79c5f
Issue #19588: Fixed tests in test_random that were silently skipped most
Zachary Ware <zachary.ware@gmail.com>
parents:
83447
diff
changeset
|
246 continue |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
247 self.assertTrue(start <= self.gen.randrange(start, stop) < stop) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
248 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
249 def test_rangelimits(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
250 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
251 self.assertEqual(set(range(start,stop)), |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
252 set([self.gen.randrange(start,stop) for i in range(100)])) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
253 |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
254 def test_randrange_nonunit_step(self): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
255 rint = self.gen.randrange(0, 10, 2) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
256 self.assertIn(rint, (0, 2, 4, 6, 8)) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
257 rint = self.gen.randrange(0, 2, 2) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
258 self.assertEqual(rint, 0) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
259 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
260 def test_randrange_errors(self): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
261 raises = partial(self.assertRaises, ValueError, self.gen.randrange) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
262 # Empty range |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
263 raises(3, 3) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
264 raises(-721) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
265 raises(0, 100, -12) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
266 # Non-integer start/stop |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
267 raises(3.14159) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
268 raises(0, 2.71828) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
269 # Zero and non-integer step |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
270 raises(0, 42, 0) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
271 raises(0, 42, 3.14159) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
272 |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
273 def test_genrandbits(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
274 # Verify ranges |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
275 for k in range(1, 1000): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
276 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
277 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
278 # Verify all bits active |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
279 getbits = self.gen.getrandbits |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
280 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
281 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
282 for i in range(100): |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
283 cum |= getbits(span) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
284 self.assertEqual(cum, 2**span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
285 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
286 # Verify argument checking |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
287 self.assertRaises(TypeError, self.gen.getrandbits) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
288 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
289 self.assertRaises(ValueError, self.gen.getrandbits, 0) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
290 self.assertRaises(ValueError, self.gen.getrandbits, -1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
291 self.assertRaises(TypeError, self.gen.getrandbits, 10.1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
292 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
293 def test_randbelow_logic(self, _log=log, int=int): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
294 # check bitcount transition points: 2**i and 2**(i+1)-1 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
295 # show that: k = int(1.001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
296 # is equal to or one greater than the number of bits in n |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
297 for i in range(1, 1000): |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
298 n = 1 << i # check an exact power of two |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
299 numbits = i+1 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
300 k = int(1.00001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
301 self.assertEqual(k, numbits) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
302 self.assertEqual(n, 2**(k-1)) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
303 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
304 n += n - 1 # check 1 below the next power of two |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
305 k = int(1.00001 + _log(n, 2)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
306 self.assertIn(k, [numbits, numbits+1]) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
307 self.assertTrue(2**k > n > 2**(k-2)) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
308 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
309 n -= n >> 15 # check a little farther below the next power of two |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
310 k = int(1.00001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
311 self.assertEqual(k, numbits) # note the stronger assertion |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
312 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
313 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
314 |
|
83447
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
315 class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase): |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
316 gen = random.Random() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
317 |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
318 def test_guaranteed_stable(self): |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
319 # These sequences are guaranteed to stay the same across versions of python |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
320 self.gen.seed(3456147, version=1) |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
321 self.assertEqual([self.gen.random().hex() for i in range(4)], |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
322 ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1', |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
323 '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1']) |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
324 self.gen.seed("the quick brown fox", version=2) |
|
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
325 self.assertEqual([self.gen.random().hex() for i in range(4)], |
|
66786
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64524
diff
changeset
|
326 ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4', |
|
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64524
diff
changeset
|
327 '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1']) |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
328 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
329 def test_setstate_first_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
330 self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
331 |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
332 def test_setstate_middle_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
333 # Wrong type, s/b tuple |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
334 self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
335 # Wrong length, s/b 625 |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
336 self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
337 # Wrong type, s/b tuple of 625 ints |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
338 self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
339 # Last element s/b an int also |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
340 self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
341 |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
342 # Little trick to make "tuple(x % (2**32) for x in internalstate)" |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
343 # raise ValueError. I cannot think of a simple way to achieve this, so |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
344 # I am opting for using a generator as the middle argument of setstate |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
345 # which attempts to cast a NaN to integer. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
346 state_values = self.gen.getstate()[1] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
347 state_values = list(state_values) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
348 state_values[-1] = float('nan') |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
349 state = (int(x) for x in state_values) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
350 self.assertRaises(TypeError, self.gen.setstate, (2, state, None)) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
351 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
352 def test_referenceImplementation(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
353 # Compare the python implementation with results from the original |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
354 # code. Create 2000 53-bit precision random floats. Compare only |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
355 # the last ten entries to show that the independent implementations |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
356 # are tracking. Here is the main() function needed to create the |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
357 # list of expected random numbers: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
358 # void main(void){ |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
359 # int i; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
360 # unsigned long init[4]={61731, 24903, 614, 42143}, length=4; |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
361 # init_by_array(init, length); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
362 # for (i=0; i<2000; i++) { |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
363 # printf("%.15f ", genrand_res53()); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
364 # if (i%5==4) printf("\n"); |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
365 # } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
366 # } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
367 expected = [0.45839803073713259, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
368 0.86057815201978782, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
369 0.92848331726782152, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
370 0.35932681119782461, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
371 0.081823493762449573, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
372 0.14332226470169329, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
373 0.084297823823520024, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
374 0.53814864671831453, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
375 0.089215024911993401, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
376 0.78486196105372907] |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
377 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
378 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
379 actual = self.randomlist(2000)[-10:] |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
380 for a, e in zip(actual, expected): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
381 self.assertAlmostEqual(a,e,places=14) |
|
23380
9be01b7b6c80
random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
382 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
383 def test_strong_reference_implementation(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
384 # Like test_referenceImplementation, but checks for exact bit-level |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
385 # equality. This should pass on any box where C double contains |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
386 # at least 53 bits of precision (the underlying algorithm suffers |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
387 # no rounding errors -- all results are exact). |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
388 from math import ldexp |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
389 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
390 expected = [0x0eab3258d2231f, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
391 0x1b89db315277a5, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
392 0x1db622a5518016, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
393 0x0b7f9af0d575bf, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
394 0x029e4c4db82240, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
395 0x04961892f5d673, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
396 0x02b291598e4589, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
397 0x11388382c15694, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
398 0x02dad977c9e1fe, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
399 0x191d96d4d334c6] |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
400 self.gen.seed(61731 + (24903<<32) + (614<<64) + (42143<<96)) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
401 actual = self.randomlist(2000)[-10:] |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
402 for a, e in zip(actual, expected): |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
403 self.assertEqual(int(ldexp(a, 53)), e) |
|
23380
9be01b7b6c80
random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
404 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
405 def test_long_seed(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
406 # This is most interesting to run in debug mode, just to make sure |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
407 # nothing blows up. Under the covers, a dynamically resized array |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
408 # is allocated, consuming space proportional to the number of bits |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
409 # in the seed. Unfortunately, that's a quadratic-time algorithm, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
410 # so don't make this horribly big. |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
411 seed = (1 << (10000 * 8)) - 1 # about 10K bytes |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
412 self.gen.seed(seed) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
413 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
414 def test_53_bits_per_float(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
415 # This should pass whenever a C double has 53 bit precision. |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
416 span = 2 ** 53 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
417 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
418 for i in range(100): |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
419 cum |= int(self.gen.random() * span) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
420 self.assertEqual(cum, span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
421 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
422 def test_bigrand(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
423 # The randrange routine should build-up the required number of bits |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
424 # in stages so that all bit positions are active. |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
425 span = 2 ** 500 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
426 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
427 for i in range(100): |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
428 r = self.gen.randrange(span) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
429 self.assertTrue(0 <= r < span) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
430 cum |= r |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
431 self.assertEqual(cum, span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
432 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
433 def test_bigrand_ranges(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
434 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
|
87597
c65882d79c5f
Issue #19588: Fixed tests in test_random that were silently skipped most
Zachary Ware <zachary.ware@gmail.com>
parents:
83447
diff
changeset
|
435 start = self.gen.randrange(2 ** (i-2)) |
|
c65882d79c5f
Issue #19588: Fixed tests in test_random that were silently skipped most
Zachary Ware <zachary.ware@gmail.com>
parents:
83447
diff
changeset
|
436 stop = self.gen.randrange(2 ** i) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
437 if stop <= start: |
|
87597
c65882d79c5f
Issue #19588: Fixed tests in test_random that were silently skipped most
Zachary Ware <zachary.ware@gmail.com>
parents:
83447
diff
changeset
|
438 continue |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
439 self.assertTrue(start <= self.gen.randrange(start, stop) < stop) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
440 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
441 def test_rangelimits(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
442 for start, stop in [(-2,0), (-(2**60)-2,-(2**60)), (2**60,2**60+2)]: |
|
30929
54ff68e973ee
* Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
30649
diff
changeset
|
443 self.assertEqual(set(range(start,stop)), |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
444 set([self.gen.randrange(start,stop) for i in range(100)])) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
445 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
446 def test_genrandbits(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
447 # Verify cross-platform repeatability |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
448 self.gen.seed(1234567) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
449 self.assertEqual(self.gen.getrandbits(100), |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
450 97904845777343510404718956115) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
451 # Verify ranges |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
452 for k in range(1, 1000): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
453 self.assertTrue(0 <= self.gen.getrandbits(k) < 2**k) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
454 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
455 # Verify all bits active |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
456 getbits = self.gen.getrandbits |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
457 for span in [1, 2, 3, 4, 31, 32, 32, 52, 53, 54, 119, 127, 128, 129]: |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
458 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
459 for i in range(100): |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
460 cum |= getbits(span) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
461 self.assertEqual(cum, 2**span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
462 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
463 # Verify argument checking |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
464 self.assertRaises(TypeError, self.gen.getrandbits) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
465 self.assertRaises(TypeError, self.gen.getrandbits, 'a') |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
466 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
467 self.assertRaises(ValueError, self.gen.getrandbits, 0) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
468 self.assertRaises(ValueError, self.gen.getrandbits, -1) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
469 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
470 def test_randbelow_logic(self, _log=log, int=int): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
471 # check bitcount transition points: 2**i and 2**(i+1)-1 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
472 # show that: k = int(1.001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
473 # is equal to or one greater than the number of bits in n |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
474 for i in range(1, 1000): |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
475 n = 1 << i # check an exact power of two |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
476 numbits = i+1 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
477 k = int(1.00001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
478 self.assertEqual(k, numbits) |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
479 self.assertEqual(n, 2**(k-1)) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
480 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
481 n += n - 1 # check 1 below the next power of two |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
482 k = int(1.00001 + _log(n, 2)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
483 self.assertIn(k, [numbits, numbits+1]) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
484 self.assertTrue(2**k > n > 2**(k-2)) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
485 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
486 n -= n >> 15 # check a little farther below the next power of two |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
487 k = int(1.00001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
488 self.assertEqual(k, numbits) # note the stronger assertion |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
489 self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
490 |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
491 @unittest.mock.patch('random.Random.random') |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
492 def test_randbelow_overriden_random(self, random_mock): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
493 # Random._randbelow() can only use random() when the built-in one |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
494 # has been overridden but no new getrandbits() method was supplied. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
495 random_mock.side_effect = random.SystemRandom().random |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
496 maxsize = 1<<random.BPF |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
497 with warnings.catch_warnings(): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
498 warnings.simplefilter("ignore", UserWarning) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
499 # Population range too large (n >= maxsize) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
500 self.gen._randbelow(maxsize+1, maxsize = maxsize) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
501 self.gen._randbelow(5640, maxsize = maxsize) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
502 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
503 # This might be going too far to test a single line, but because of our |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
504 # noble aim of achieving 100% test coverage we need to write a case in |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
505 # which the following line in Random._randbelow() gets executed: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
506 # |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
507 # rem = maxsize % n |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
508 # limit = (maxsize - rem) / maxsize |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
509 # r = random() |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
510 # while r >= limit: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
511 # r = random() # <== *This line* <==< |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
512 # |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
513 # Therefore, to guarantee that the while loop is executed at least |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
514 # once, we need to mock random() so that it returns a number greater |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
515 # than 'limit' the first time it gets called. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
516 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
517 n = 42 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
518 epsilon = 0.01 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
519 limit = (maxsize - (maxsize % n)) / maxsize |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
520 random_mock.side_effect = [limit + epsilon, limit - epsilon] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
521 self.gen._randbelow(n, maxsize = maxsize) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
522 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
523 def test_randrange_bug_1590891(self): |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
524 start = 1000000000000 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
525 stop = -100000000000000000000 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
526 step = -200 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
527 x = self.gen.randrange(start, stop, step) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
528 self.assertTrue(stop < x <= start) |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
529 self.assertEqual((x+stop)%step, 0) |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
530 |
|
51738
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
531 def gamma(z, sqrt2pi=(2.0*pi)**0.5): |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
532 # Reflection to right half of complex plane |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
533 if z < 0.5: |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
534 return pi / sin(pi*z) / gamma(1.0-z) |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
535 # Lanczos approximation with g=7 |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
536 az = z + (7.0 - 0.5) |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
537 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([ |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
538 0.9999999999995183, |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
539 676.5203681218835 / z, |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
540 -1259.139216722289 / (z+1.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
541 771.3234287757674 / (z+2.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
542 -176.6150291498386 / (z+3.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
543 12.50734324009056 / (z+4.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
544 -0.1385710331296526 / (z+5.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
545 0.9934937113930748e-05 / (z+6.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
546 0.1659470187408462e-06 / (z+7.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
547 ]) |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
548 |
| 27092 | 549 class TestDistributions(unittest.TestCase): |
| 550 def test_zeroinputs(self): | |
| 551 # Verify that distributions can handle a series of zero inputs' | |
| 552 g = random.Random() | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
553 x = [g.random() for i in range(50)] + [0.0]*5 |
| 27092 | 554 g.random = x[:].pop; g.uniform(1,10) |
| 555 g.random = x[:].pop; g.paretovariate(1.0) | |
| 556 g.random = x[:].pop; g.expovariate(1.0) | |
| 557 g.random = x[:].pop; g.weibullvariate(1.0, 1.0) | |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
558 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0) |
| 27092 | 559 g.random = x[:].pop; g.normalvariate(0.0, 1.0) |
| 560 g.random = x[:].pop; g.gauss(0.0, 1.0) | |
| 561 g.random = x[:].pop; g.lognormvariate(0.0, 1.0) | |
| 562 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) | |
| 563 g.random = x[:].pop; g.gammavariate(0.01, 1.0) | |
| 564 g.random = x[:].pop; g.gammavariate(1.0, 1.0) | |
| 565 g.random = x[:].pop; g.gammavariate(200.0, 1.0) | |
| 566 g.random = x[:].pop; g.betavariate(3.0, 3.0) | |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
567 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) |
| 27092 | 568 |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
569 def test_avg_std(self): |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
570 # Use integration to test distribution average and standard deviation. |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
571 # Only works for distributions which do not consume variates in pairs |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
572 g = random.Random() |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
573 N = 5000 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
574 x = [i/float(N) for i in range(1,N)] |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
575 for variate, args, mu, sigmasqrd in [ |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
576 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), |
|
46034
0a485593ae1c
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44665
diff
changeset
|
577 (g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0), |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
578 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
579 (g.vonmisesvariate, (1.23, 0), pi, pi**2/3), |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
580 (g.paretovariate, (5.0,), 5.0/(5.0-1), |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
581 5.0/((5.0-1)**2*(5.0-2))), |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
582 (g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0), |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
583 gamma(1+2/3.0)-gamma(1+1/3.0)**2) ]: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
584 g.random = x[:].pop |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
585 y = [] |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
586 for i in range(len(x)): |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
587 try: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
588 y.append(variate(*args)) |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
589 except IndexError: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
590 pass |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
591 s1 = s2 = 0 |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
592 for e in y: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
593 s1 += e |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
594 s2 += (e - mu) ** 2 |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
595 N = len(y) |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
596 self.assertAlmostEqual(s1/N, mu, places=2, |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
597 msg='%s%r' % (variate.__name__, args)) |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
598 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2, |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
599 msg='%s%r' % (variate.__name__, args)) |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
600 |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
601 def test_constant(self): |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
602 g = random.Random() |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
603 N = 100 |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
604 for variate, args, expected in [ |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
605 (g.uniform, (10.0, 10.0), 10.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
606 (g.triangular, (10.0, 10.0), 10.0), |
|
83288
e948154af406
Revert changes for #13355 by request from Raymond Hettinger
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
83279
diff
changeset
|
607 #(g.triangular, (10.0, 10.0, 10.0), 10.0), |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
608 (g.expovariate, (float('inf'),), 0.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
609 (g.vonmisesvariate, (3.0, float('inf')), 3.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
610 (g.gauss, (10.0, 0.0), 10.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
611 (g.lognormvariate, (0.0, 0.0), 1.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
612 (g.lognormvariate, (-float('inf'), 0.0), 0.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
613 (g.normalvariate, (10.0, 0.0), 10.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
614 (g.paretovariate, (float('inf'),), 1.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
615 (g.weibullvariate, (10.0, float('inf')), 10.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
616 (g.weibullvariate, (0.0, 10.0), 0.0), |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
617 ]: |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
618 for i in range(N): |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
619 self.assertEqual(variate(*args), expected) |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
620 |
|
82130
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
621 def test_von_mises_range(self): |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
622 # Issue 17149: von mises variates were not consistently in the |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
623 # range [0, 2*PI]. |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
624 g = random.Random() |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
625 N = 100 |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
626 for mu in 0.0, 0.1, 3.1, 6.2: |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
627 for kappa in 0.0, 2.3, 500.0: |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
628 for _ in range(N): |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
629 sample = g.vonmisesvariate(mu, kappa) |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
630 self.assertTrue( |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
631 0 <= sample <= random.TWOPI, |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
632 msg=("vonmisesvariate({}, {}) produced a result {} out" |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
633 " of range [0, 2*pi]").format(mu, kappa, sample)) |
|
41e97652a9f9
Issue #17149: Fix random.vonmisesvariate to always return results in [0, 2*math.pi].
Mark Dickinson <dickinsm@gmail.com>
parents:
66786
diff
changeset
|
634 |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
635 def test_von_mises_large_kappa(self): |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
636 # Issue #17141: vonmisesvariate() was hang for large kappas |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
637 random.vonmisesvariate(0, 1e15) |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
638 random.vonmisesvariate(0, 1e100) |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
639 |
|
83062
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
640 def test_gammavariate_errors(self): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
641 # Both alpha and beta must be > 0.0 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
642 self.assertRaises(ValueError, random.gammavariate, -1, 3) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
643 self.assertRaises(ValueError, random.gammavariate, 0, 2) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
644 self.assertRaises(ValueError, random.gammavariate, 2, 0) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
645 self.assertRaises(ValueError, random.gammavariate, 1, -3) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
646 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
647 @unittest.mock.patch('random.Random.random') |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
648 def test_gammavariate_full_code_coverage(self, random_mock): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
649 # There are three different possibilities in the current implementation |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
650 # of random.gammavariate(), depending on the value of 'alpha'. What we |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
651 # are going to do here is to fix the values returned by random() to |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
652 # generate test cases that provide 100% line coverage of the method. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
653 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
654 # #1: alpha > 1.0: we want the first random number to be outside the |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
655 # [1e-7, .9999999] range, so that the continue statement executes |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
656 # once. The values of u1 and u2 will be 0.5 and 0.3, respectively. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
657 random_mock.side_effect = [1e-8, 0.5, 0.3] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
658 returned_value = random.gammavariate(1.1, 2.3) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
659 self.assertAlmostEqual(returned_value, 2.53) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
660 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
661 # #2: alpha == 1: first random number less than 1e-7 to that the body |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
662 # of the while loop executes once. Then random.random() returns 0.45, |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
663 # which causes while to stop looping and the algorithm to terminate. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
664 random_mock.side_effect = [1e-8, 0.45] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
665 returned_value = random.gammavariate(1.0, 3.14) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
666 self.assertAlmostEqual(returned_value, 2.507314166123803) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
667 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
668 # #3: 0 < alpha < 1. This is the most complex region of code to cover, |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
669 # as there are multiple if-else statements. Let's take a look at the |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
670 # source code, and determine the values that we need accordingly: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
671 # |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
672 # while 1: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
673 # u = random() |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
674 # b = (_e + alpha)/_e |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
675 # p = b*u |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
676 # if p <= 1.0: # <=== (A) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
677 # x = p ** (1.0/alpha) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
678 # else: # <=== (B) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
679 # x = -_log((b-p)/alpha) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
680 # u1 = random() |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
681 # if p > 1.0: # <=== (C) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
682 # if u1 <= x ** (alpha - 1.0): # <=== (D) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
683 # break |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
684 # elif u1 <= _exp(-x): # <=== (E) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
685 # break |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
686 # return x * beta |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
687 # |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
688 # First, we want (A) to be True. For that we need that: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
689 # b*random() <= 1.0 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
690 # r1 = random() <= 1.0 / b |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
691 # |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
692 # We now get to the second if-else branch, and here, since p <= 1.0, |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
693 # (C) is False and we take the elif branch, (E). For it to be True, |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
694 # so that the break is executed, we need that: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
695 # r2 = random() <= _exp(-x) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
696 # r2 <= _exp(-(p ** (1.0/alpha))) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
697 # r2 <= _exp(-((b*r1) ** (1.0/alpha))) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
698 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
699 _e = random._e |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
700 _exp = random._exp |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
701 _log = random._log |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
702 alpha = 0.35 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
703 beta = 1.45 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
704 b = (_e + alpha)/_e |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
705 epsilon = 0.01 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
706 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
707 r1 = 0.8859296441566 # 1.0 / b |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
708 r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha))) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
709 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
710 # These four "random" values result in the following trace: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
711 # (A) True, (E) False --> [next iteration of while] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
712 # (A) True, (E) True --> [while loop breaks] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
713 random_mock.side_effect = [r1, r2 + epsilon, r1, r2] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
714 returned_value = random.gammavariate(alpha, beta) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
715 self.assertAlmostEqual(returned_value, 1.4499999999997544) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
716 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
717 # Let's now make (A) be False. If this is the case, when we get to the |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
718 # second if-else 'p' is greater than 1, so (C) evaluates to True. We |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
719 # now encounter a second if statement, (D), which in order to execute |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
720 # must satisfy the following condition: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
721 # r2 <= x ** (alpha - 1.0) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
722 # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
723 # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
724 r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
725 r2 = 0.9445400408898141 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
726 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
727 # And these four values result in the following trace: |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
728 # (B) and (C) True, (D) False --> [next iteration of while] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
729 # (B) and (C) True, (D) True [while loop breaks] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
730 random_mock.side_effect = [r1, r2 + epsilon, r1, r2] |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
731 returned_value = random.gammavariate(alpha, beta) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
732 self.assertAlmostEqual(returned_value, 1.5830349561760781) |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
733 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
734 @unittest.mock.patch('random.Random.gammavariate') |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
735 def test_betavariate_return_zero(self, gammavariate_mock): |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
736 # betavariate() returns zero when the Gamma distribution |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
737 # that it uses internally returns this same value. |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
738 gammavariate_mock.return_value = 0.0 |
|
2a340f963518
#17492: Additional tests for random module.
R David Murray <rdmurray@bitdance.com>
parents:
82142
diff
changeset
|
739 self.assertEqual(0.0, random.betavariate(2.71828, 3.14159)) |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
740 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
741 class TestModule(unittest.TestCase): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
742 def testMagicConstants(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
743 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
744 self.assertAlmostEqual(random.TWOPI, 6.28318530718) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
745 self.assertAlmostEqual(random.LOG4, 1.38629436111989) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
746 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
747 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
748 def test__all__(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
749 # tests validity but not completeness of the __all__ list |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
750 self.assertTrue(set(random.__all__) <= set(dir(random))) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
751 |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
752 def test_random_subclass_with_kwargs(self): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
753 # SF bug #1486663 -- this used to erroneously raise a TypeError |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
754 class Subclass(random.Random): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
755 def __init__(self, newarg=None): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
756 random.Random.__init__(self) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
757 Subclass(newarg=1) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
758 |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
759 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
760 if __name__ == "__main__": |
|
83447
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
761 unittest.main() |
