Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_random.py @ 83447:678564fc0962 3.3
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 19 Apr 2013 05:45:27 +0300 |
| parents | e948154af406 |
| children | c65882d79c5f ac352d7d8058 |
| 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 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
4 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
|
5 import time |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
6 import pickle |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
7 import warnings |
|
59615
b93cfafcdcad
Merged revisions 78093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
59522
diff
changeset
|
8 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
|
9 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
|
10 |
|
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
|
11 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
|
12 # 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
|
13 # 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
|
14 # 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
|
15 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
16 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
|
17 """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
|
18 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
|
19 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
36 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
|
37 # 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
|
38 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
|
39 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
|
40 return -1729 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
41 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
|
42 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
|
43 self.gen.seed(arg) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 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
|
48 |
|
64517
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
49 def test_choice(self): |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
50 choice = self.gen.choice |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
51 with self.assertRaises(IndexError): |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
52 choice([]) |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
53 self.assertEqual(choice([50]), 50) |
|
e83af141dffd
Fix corner case for Random.choice() and add tests.
Raymond Hettinger <python@rcn.com>
parents:
64512
diff
changeset
|
54 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
|
55 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
56 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
|
57 # 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
|
58 # 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
|
59 N = 100 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
60 population = range(N) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
61 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
|
62 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
|
63 self.assertEqual(len(s), k) |
|
30929
54ff68e973ee
* Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
30649
diff
changeset
|
64 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
|
65 self.assertEqual(len(uniq), k) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
66 self.assertTrue(uniq <= set(population)) |
|
27081
3199e974f55a
Test an edge case for sample().
Raymond Hettinger <python@rcn.com>
parents:
26829
diff
changeset
|
67 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
68 |
|
27279
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
69 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
|
70 # 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
|
71 # 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
|
72 n = 5 |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
73 pop = range(n) |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 return n * factorial(n - 1) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
79 for k in range(n): |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33481
diff
changeset
|
80 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
|
81 perms = {} |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
82 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
|
83 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
|
84 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
|
85 break |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
86 else: |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
87 self.fail() |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
88 |
|
30346
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
89 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
|
90 # 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
|
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
|
97 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
|
98 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
|
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_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
|
101 # 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
|
102 # 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
|
103 # 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
|
104 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 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
|
109 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
114 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
|
115 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
|
116 |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
117 def test_pickling(self): |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
118 state = pickle.dumps(self.gen) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
119 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
|
120 newgen = pickle.loads(state) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
121 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
|
122 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
|
123 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
124 def test_bug_1727780(self): |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
125 # 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
|
126 # 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
|
127 # 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
|
128 files = [("randv2_32.pck", 780), |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
129 ("randv2_64.pck", 866), |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
130 ("randv3.pck", 343)] |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
131 for file, value in files: |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46034
diff
changeset
|
132 f = open(support.findfile(file),"rb") |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
133 r = pickle.load(f) |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43285
diff
changeset
|
134 f.close() |
|
64512
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
135 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
|
136 |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
137 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
|
138 # 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
|
139 # 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
|
140 n = 100000 |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
141 randrange = self.gen.randrange |
|
c2f8418a0e14
Issues #7889, #9025 and #9379: Improvements to the random module.
Raymond Hettinger <python@rcn.com>
parents:
64510
diff
changeset
|
142 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
|
143 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
|
144 |
|
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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 |
|
678564fc0962
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
83288
diff
changeset
|
152 @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
|
153 class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase): |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
154 gen = random.SystemRandom() |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
155 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
156 def test_autoseed(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
157 # 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
|
158 self.gen.seed() |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
159 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
160 def test_saverestore(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
161 self.assertRaises(NotImplementedError, self.gen.getstate) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
162 self.assertRaises(NotImplementedError, self.gen.setstate, None) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
163 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
164 def test_seedargs(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
165 # 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
|
166 self.gen.seed(100) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
167 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
168 def test_gauss(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
169 self.gen.gauss_next = None |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
170 self.gen.seed(100) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
171 self.assertEqual(self.gen.gauss_next, None) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
172 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
173 def test_pickling(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
174 self.assertRaises(NotImplementedError, pickle.dumps, self.gen) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
175 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
176 def test_53_bits_per_float(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
177 # 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
|
178 span = 2 ** 53 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
179 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
180 for i in range(100): |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
181 cum |= int(self.gen.random() * span) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
182 self.assertEqual(cum, span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
183 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
184 def test_bigrand(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
185 # 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
|
186 # 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
|
187 span = 2 ** 500 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
188 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
189 for i in range(100): |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
190 r = self.gen.randrange(span) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
191 self.assertTrue(0 <= r < span) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
192 cum |= r |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
193 self.assertEqual(cum, span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
194 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
195 def test_bigrand_ranges(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
196 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
197 start = self.gen.randrange(2 ** i) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
198 stop = self.gen.randrange(2 ** (i-2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
199 if stop <= start: |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
200 return |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
201 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
|
202 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
203 def test_rangelimits(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
204 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
|
205 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
|
206 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
|
207 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
208 def test_genrandbits(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
209 # Verify ranges |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
210 for k in range(1, 1000): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
211 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
|
212 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
213 # Verify all bits active |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
214 getbits = self.gen.getrandbits |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
215 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
|
216 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
217 for i in range(100): |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
218 cum |= getbits(span) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
219 self.assertEqual(cum, 2**span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
220 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
221 # Verify argument checking |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
222 self.assertRaises(TypeError, self.gen.getrandbits) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
223 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
|
224 self.assertRaises(ValueError, self.gen.getrandbits, 0) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
225 self.assertRaises(ValueError, self.gen.getrandbits, -1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
226 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
|
227 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
228 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
|
229 # 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
|
230 # 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
|
231 # 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
|
232 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
|
233 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
|
234 numbits = i+1 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
235 k = int(1.00001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
236 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
|
237 self.assertEqual(n, 2**(k-1)) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
238 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
239 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
|
240 k = int(1.00001 + _log(n, 2)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
241 self.assertIn(k, [numbits, numbits+1]) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
242 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
|
243 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
244 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
|
245 k = int(1.00001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
246 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
|
247 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
|
248 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
249 |
|
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
|
250 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
|
251 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
|
252 |
|
64510
65642f085c55
Document which part of the random module module are guaranteed.
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
253 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
|
254 # 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
|
255 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
|
256 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
|
257 ['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
|
258 '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
|
259 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
|
260 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
|
261 ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4', |
|
f4f56efa2870
Update whatsnew. Salt the random number seed.
Raymond Hettinger <python@rcn.com>
parents:
64524
diff
changeset
|
262 '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
|
263 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
264 def test_setstate_first_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
265 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
|
266 |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
267 def test_setstate_middle_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
268 # Wrong type, s/b tuple |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
269 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
|
270 # Wrong length, s/b 625 |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
271 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
|
272 # Wrong type, s/b tuple of 625 ints |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
273 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
|
274 # Last element s/b an int also |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
275 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
|
276 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
277 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
|
278 # 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
|
279 # 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
|
280 # 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
|
281 # 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
|
282 # 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
|
283 # 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
|
284 # 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
|
285 # 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
|
286 # 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
|
287 # 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
|
288 # 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
|
289 # 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
|
290 # } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
291 # } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 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
|
301 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
|
302 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
303 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
|
304 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
|
305 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
|
306 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
|
307 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
308 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
|
309 # 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
|
310 # 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
|
311 # 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
|
312 # 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
|
313 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
|
314 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
315 expected = [0x0eab3258d2231f, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
316 0x1b89db315277a5, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
317 0x1db622a5518016, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
318 0x0b7f9af0d575bf, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
319 0x029e4c4db82240, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
320 0x04961892f5d673, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
321 0x02b291598e4589, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
322 0x11388382c15694, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
323 0x02dad977c9e1fe, |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
324 0x191d96d4d334c6] |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
325 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
|
326 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
|
327 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
|
328 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
|
329 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
330 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
|
331 # 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
|
332 # 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
|
333 # 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
|
334 # 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
|
335 # 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
|
336 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
|
337 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
|
338 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
339 def test_53_bits_per_float(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
340 # 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
|
341 span = 2 ** 53 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
342 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
343 for i in range(100): |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
344 cum |= int(self.gen.random() * span) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
345 self.assertEqual(cum, span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
346 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
347 def test_bigrand(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
348 # 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
|
349 # 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
|
350 span = 2 ** 500 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
351 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
352 for i in range(100): |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
353 r = self.gen.randrange(span) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
354 self.assertTrue(0 <= r < span) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
355 cum |= r |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
356 self.assertEqual(cum, span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
357 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
358 def test_bigrand_ranges(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
359 for i in [40,80, 160, 200, 211, 250, 375, 512, 550]: |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
360 start = self.gen.randrange(2 ** i) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
361 stop = self.gen.randrange(2 ** (i-2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
362 if stop <= start: |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
363 return |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
364 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
|
365 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
366 def test_rangelimits(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
367 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
|
368 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
|
369 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
|
370 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
371 def test_genrandbits(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
372 # Verify cross-platform repeatability |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
373 self.gen.seed(1234567) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
374 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
|
375 97904845777343510404718956115) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
376 # Verify ranges |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
377 for k in range(1, 1000): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
378 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
|
379 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
380 # Verify all bits active |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
381 getbits = self.gen.getrandbits |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
382 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
|
383 cum = 0 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
384 for i in range(100): |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
385 cum |= getbits(span) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
386 self.assertEqual(cum, 2**span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
387 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
388 # Verify argument checking |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
389 self.assertRaises(TypeError, self.gen.getrandbits) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
390 self.assertRaises(TypeError, self.gen.getrandbits, 'a') |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
391 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
392 self.assertRaises(ValueError, self.gen.getrandbits, 0) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
393 self.assertRaises(ValueError, self.gen.getrandbits, -1) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
394 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
395 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
|
396 # 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
|
397 # 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
|
398 # 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
|
399 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
|
400 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
|
401 numbits = i+1 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
402 k = int(1.00001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
403 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
|
404 self.assertEqual(n, 2**(k-1)) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
405 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
406 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
|
407 k = int(1.00001 + _log(n, 2)) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
55593
diff
changeset
|
408 self.assertIn(k, [numbits, numbits+1]) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
409 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
|
410 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
411 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
|
412 k = int(1.00001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
413 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
|
414 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
|
415 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
416 def test_randrange_bug_1590891(self): |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
417 start = 1000000000000 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
418 stop = -100000000000000000000 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
419 step = -200 |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
420 x = self.gen.randrange(start, stop, step) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51738
diff
changeset
|
421 self.assertTrue(stop < x <= start) |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
422 self.assertEqual((x+stop)%step, 0) |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
423 |
|
51738
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
424 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
|
425 # 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
|
426 if z < 0.5: |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
427 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
|
428 # Lanczos approximation with g=7 |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
429 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
|
430 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
|
431 0.9999999999995183, |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
432 676.5203681218835 / z, |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
433 -1259.139216722289 / (z+1.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
434 771.3234287757674 / (z+2.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
435 -176.6150291498386 / (z+3.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
436 12.50734324009056 / (z+4.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
437 -0.1385710331296526 / (z+5.0), |
|
78e311f277a7
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
48606
diff
changeset
|
438 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
|
439 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
|
440 ]) |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
441 |
| 27092 | 442 class TestDistributions(unittest.TestCase): |
| 443 def test_zeroinputs(self): | |
| 444 # Verify that distributions can handle a series of zero inputs' | |
| 445 g = random.Random() | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
446 x = [g.random() for i in range(50)] + [0.0]*5 |
| 27092 | 447 g.random = x[:].pop; g.uniform(1,10) |
| 448 g.random = x[:].pop; g.paretovariate(1.0) | |
| 449 g.random = x[:].pop; g.expovariate(1.0) | |
| 450 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
|
451 g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0) |
| 27092 | 452 g.random = x[:].pop; g.normalvariate(0.0, 1.0) |
| 453 g.random = x[:].pop; g.gauss(0.0, 1.0) | |
| 454 g.random = x[:].pop; g.lognormvariate(0.0, 1.0) | |
| 455 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) | |
| 456 g.random = x[:].pop; g.gammavariate(0.01, 1.0) | |
| 457 g.random = x[:].pop; g.gammavariate(1.0, 1.0) | |
| 458 g.random = x[:].pop; g.gammavariate(200.0, 1.0) | |
| 459 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
|
460 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) |
| 27092 | 461 |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
462 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
|
463 # 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
|
464 # 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
|
465 g = random.Random() |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
466 N = 5000 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
467 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
|
468 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
|
469 (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
|
470 (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
|
471 (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
|
472 (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
|
473 (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
|
474 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
|
475 (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
|
476 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
|
477 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
|
478 y = [] |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
479 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
|
480 try: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
481 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
|
482 except IndexError: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
483 pass |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
484 s1 = s2 = 0 |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
485 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
|
486 s1 += e |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
487 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
|
488 N = len(y) |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
489 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
|
490 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
|
491 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
|
492 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
|
493 |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
494 def test_constant(self): |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
495 g = random.Random() |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
496 N = 100 |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
497 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
|
498 (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
|
499 (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
|
500 #(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
|
501 (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
|
502 (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
|
503 (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
|
504 (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
|
505 (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
|
506 (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
|
507 (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
|
508 (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
|
509 (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
|
510 ]: |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
511 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
|
512 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
|
513 |
|
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
|
514 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
|
515 # 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
|
516 # 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
|
517 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
|
518 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
|
519 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
|
520 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
|
521 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
|
522 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
|
523 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
|
524 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
|
525 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
|
526 " 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
|
527 |
|
82140
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
528 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
|
529 # 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
|
530 random.vonmisesvariate(0, 1e15) |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
531 random.vonmisesvariate(0, 1e100) |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
532 |
|
d94b73c95646
Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
Serhiy Storchaka <storchaka@gmail.com>
parents:
82130
diff
changeset
|
533 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 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
|
540 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
541 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
|
542 # 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
|
543 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
|
544 |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
545 def test_random_subclass_with_kwargs(self): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
546 # 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
|
547 class Subclass(random.Random): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
548 def __init__(self, newarg=None): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
549 random.Random.__init__(self) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
550 Subclass(newarg=1) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
551 |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
552 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
553 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
|
554 unittest.main() |
