Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_random.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 6df0b4ed8617 |
| children | 6a3d18cede49 |
| rev | line source |
|---|---|
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
1 #!/usr/bin/env python |
|
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 |
|
58817
70e115c34f97
Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents:
58669
diff
changeset
|
8 from math import log, exp, pi, fsum, sin |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63553
diff
changeset
|
9 from functools import reduce |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
23380
diff
changeset
|
10 from test import test_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
|
11 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
12 class TestBasicOps(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
|
13 # 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
|
14 # 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
|
15 # 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
|
16 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
17 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
|
18 """Helper function to make a list of 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
|
19 return [self.gen.random() for i in xrange(n)] |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
20 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
37 def test_seedargs(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
38 for arg in [None, 0, 0L, 1, 1L, -1, -1L, 10**20, -(10**20), |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
39 3.14, 1+2j, 'a', tuple('abc')]: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
40 self.gen.seed(arg) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
41 for arg in [range(3), dict(one=1)]: |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
42 self.assertRaises(TypeError, self.gen.seed, arg) |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
43 self.assertRaises(TypeError, self.gen.seed, 1, 2) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
44 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
|
45 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
46 def test_jumpahead(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
47 self.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
|
48 state1 = 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
|
49 self.gen.jumpahead(100) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
50 state2 = self.gen.getstate() # s/b distinct from state1 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
51 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
|
52 self.gen.jumpahead(100) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
53 state3 = self.gen.getstate() # s/b distinct from state2 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
54 self.assertNotEqual(state2, state3) |
|
23380
9be01b7b6c80
random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
55 |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58817
diff
changeset
|
56 with test_support.check_py3k_warnings(quiet=True): |
|
63553
7c3b849d181a
Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
49099
diff
changeset
|
57 self.assertRaises(TypeError, self.gen.jumpahead) # needs an arg |
|
7c3b849d181a
Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
49099
diff
changeset
|
58 self.assertRaises(TypeError, self.gen.jumpahead, 2, 3) # too many |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
59 |
|
77877
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
60 def test_jumpahead_produces_valid_state(self): |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
61 # From http://bugs.python.org/issue14591. |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
62 self.gen.seed(199210368) |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
63 self.gen.jumpahead(13550674232554645900) |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
64 for i in range(500): |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
65 val = self.gen.random() |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
66 self.assertLess(val, 1.0) |
|
6df0b4ed8617
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
Mark Dickinson <mdickinson@enthought.com>
parents:
68256
diff
changeset
|
67 |
|
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 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
|
69 # 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
|
70 # 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
|
71 N = 100 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
72 population = xrange(N) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
73 for k in xrange(N+1): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
74 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
|
75 self.assertEqual(len(s), k) |
|
30929
54ff68e973ee
* Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
30649
diff
changeset
|
76 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
|
77 self.assertEqual(len(uniq), k) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
78 self.assertTrue(uniq <= set(population)) |
|
27081
3199e974f55a
Test an edge case for sample().
Raymond Hettinger <python@rcn.com>
parents:
26829
diff
changeset
|
79 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
|
80 |
|
27279
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
81 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
|
82 # 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
|
83 # 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
|
84 n = 5 |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
85 pop = range(n) |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
86 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
|
87 def factorial(n): |
| 27668 | 88 return reduce(int.__mul__, xrange(1, n), 1) |
|
27279
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
89 for k in xrange(n): |
|
33597
355b15e4a4da
Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents:
33481
diff
changeset
|
90 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
|
91 perms = {} |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
92 for i in xrange(trials): |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
93 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
|
94 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
|
95 break |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
96 else: |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
97 self.fail() |
|
9de064c7a247
* Migrate sample distribution test from random.py to test_random.py.
Raymond Hettinger <python@rcn.com>
parents:
27094
diff
changeset
|
98 |
|
30346
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
99 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
|
100 # 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
|
101 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
|
102 self.gen.sample(range(20), 2) |
|
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
103 self.gen.sample(xrange(20), 2) |
|
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
104 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
|
105 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) |
|
37360
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
106 |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
107 def test_sample_on_dicts(self): |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
108 self.gen.sample(dict.fromkeys('abcdefghijklmnopqrst'), 2) |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
109 |
|
37293
1ca012ce427e
SF bug #1460340: random.sample can raise KeyError
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
110 # SF bug #1460340 -- random.sample can raise KeyError |
|
1ca012ce427e
SF bug #1460340: random.sample can raise KeyError
Raymond Hettinger <python@rcn.com>
parents:
33597
diff
changeset
|
111 a = dict.fromkeys(range(10)+range(10,100,2)+range(100,110)) |
|
37360
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
112 self.gen.sample(a, 3) |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
113 |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
114 # A followup to bug #1460340: sampling from a dict could return |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
115 # a subset of its keys or of its values, depending on the size of |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
116 # the subset requested. |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
117 N = 30 |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
118 d = dict((i, complex(i, i)) for i in xrange(N)) |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
119 for k in xrange(N+1): |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
120 samp = self.gen.sample(d, k) |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
121 # Verify that we got ints back (keys); the values are complex. |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
122 for x in samp: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
123 self.assertTrue(type(x) is int) |
|
37360
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
124 samp.sort() |
|
98251780236d
Another crack at bug #1460340: make random.sample(dict)
Tim Peters <tim.peters@gmail.com>
parents:
37293
diff
changeset
|
125 self.assertEqual(samp, range(N)) |
|
30346
aaf6b6a8510b
SF bug #801342: Bug (documentation or real, your choice) in random.sample.
Raymond Hettinger <python@rcn.com>
parents:
30114
diff
changeset
|
126 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
127 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
|
128 # 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
|
129 # 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
|
130 # 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
|
131 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 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
|
136 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
141 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
|
142 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
|
143 |
|
29586
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
144 def test_pickling(self): |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
145 state = pickle.dumps(self.gen) |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
146 origseq = [self.gen.random() for i in xrange(10)] |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
147 newgen = pickle.loads(state) |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
148 restoredseq = [newgen.random() for i in xrange(10)] |
|
8b163370372d
SF bug #759889: Pickling of Random is broken
Raymond Hettinger <python@rcn.com>
parents:
28944
diff
changeset
|
149 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
|
150 |
|
44122
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
151 def test_bug_1727780(self): |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
152 # verify that version-2-pickles can be loaded |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
153 # fine, whether they are created on 32-bit or 64-bit |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
154 # platforms, and that version-3-pickles load fine. |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
155 files = [("randv2_32.pck", 780), |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
156 ("randv2_64.pck", 866), |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
157 ("randv3.pck", 343)] |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
158 for file, value in files: |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
159 f = open(test_support.findfile(file),"rb") |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
160 r = pickle.load(f) |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
161 f.close() |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
162 self.assertEqual(r.randrange(1000), value) |
|
97210d863d12
Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents:
42584
diff
changeset
|
163 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
164 class WichmannHill_TestBasicOps(TestBasicOps): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
165 gen = random.WichmannHill() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
166 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
167 def test_setstate_first_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
168 self.assertRaises(ValueError, self.gen.setstate, (2, None, None)) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
169 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
170 def test_strong_jumpahead(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
171 # tests that jumpahead(n) semantics correspond to n calls to random() |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
172 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
|
173 s = 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
|
174 self.gen.jumpahead(N) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
175 r1 = 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
|
176 # now do it the slow way |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
177 self.gen.setstate(s) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
178 for i in xrange(N): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
179 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
|
180 r2 = 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
|
181 self.assertEqual(r1, r2) |
|
23380
9be01b7b6c80
random.gauss() uses a piece of hidden state used by nothing else,
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
182 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
183 def test_gauss_with_whseed(self): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
184 # 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
|
185 # 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
|
186 # 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
|
187 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
188 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
|
189 self.gen.whseed(seed) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
190 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
|
191 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
|
192 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
193 self.gen.whseed(seed) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
194 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
|
195 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
|
196 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
197 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
|
198 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
|
199 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
200 def test_bigrand(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
201 # Verify warnings are raised when randrange is too large for random() |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
48101
diff
changeset
|
202 with warnings.catch_warnings(): |
|
40455
480789b3294d
Add test.test_support.guard_warnings_filter . This function returns a context
Brett Cannon <bcannon@gmail.com>
parents:
37360
diff
changeset
|
203 warnings.filterwarnings("error", "Underlying random") |
|
480789b3294d
Add test.test_support.guard_warnings_filter . This function returns a context
Brett Cannon <bcannon@gmail.com>
parents:
37360
diff
changeset
|
204 self.assertRaises(UserWarning, self.gen.randrange, 2**60) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
205 |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
206 class SystemRandom_TestBasicOps(TestBasicOps): |
|
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
207 gen = random.SystemRandom() |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
208 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
209 def test_autoseed(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
210 # 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
|
211 self.gen.seed() |
|
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 def test_saverestore(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
214 self.assertRaises(NotImplementedError, self.gen.getstate) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
215 self.assertRaises(NotImplementedError, self.gen.setstate, None) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
216 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
217 def test_seedargs(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
218 # 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
|
219 self.gen.seed(100) |
|
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 def test_jumpahead(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
222 # 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
|
223 self.gen.jumpahead(100) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
224 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
225 def test_gauss(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
226 self.gen.gauss_next = None |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
227 self.gen.seed(100) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
228 self.assertEqual(self.gen.gauss_next, None) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
229 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
230 def test_pickling(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
231 self.assertRaises(NotImplementedError, pickle.dumps, self.gen) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
232 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
233 def test_53_bits_per_float(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
234 # 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
|
235 span = 2 ** 53 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
236 cum = 0 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
237 for i in xrange(100): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
238 cum |= int(self.gen.random() * span) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
239 self.assertEqual(cum, span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
240 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
241 def test_bigrand(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
242 # 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
|
243 # 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
|
244 span = 2 ** 500 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
245 cum = 0 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
246 for i in xrange(100): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
247 r = self.gen.randrange(span) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
248 self.assertTrue(0 <= r < span) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
249 cum |= r |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
250 self.assertEqual(cum, span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
251 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
252 def test_bigrand_ranges(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
253 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
|
254 start = self.gen.randrange(2 ** i) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
255 stop = self.gen.randrange(2 ** (i-2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
256 if stop <= start: |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
257 return |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
258 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
|
259 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
260 def test_rangelimits(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
261 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
|
262 self.assertEqual(set(range(start,stop)), |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
263 set([self.gen.randrange(start,stop) for i in xrange(100)])) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
264 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
265 def test_genrandbits(self): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
266 # Verify ranges |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
267 for k in xrange(1, 1000): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
268 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
|
269 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
270 # Verify all bits active |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
271 getbits = self.gen.getrandbits |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
272 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
|
273 cum = 0 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
274 for i in xrange(100): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
275 cum |= getbits(span) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
276 self.assertEqual(cum, 2**span-1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
277 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
278 # Verify argument checking |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
279 self.assertRaises(TypeError, self.gen.getrandbits) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
280 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
|
281 self.assertRaises(ValueError, self.gen.getrandbits, 0) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
282 self.assertRaises(ValueError, self.gen.getrandbits, -1) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
283 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
|
284 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
285 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
|
286 # 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
|
287 # 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
|
288 # is equal to or one greater than the number of bits in n |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
289 for i in xrange(1, 1000): |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
290 n = 1L << i # check an exact power of two |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
291 numbits = i+1 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
292 k = int(1.00001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
293 self.assertEqual(k, numbits) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
294 self.assertTrue(n == 2**(k-1)) |
|
33329
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
295 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
296 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
|
297 k = int(1.00001 + _log(n, 2)) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
298 self.assertIn(k, [numbits, numbits+1]) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
299 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
|
300 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
301 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
|
302 k = int(1.00001 + _log(n, 2)) |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
303 self.assertEqual(k, numbits) # note the stronger assertion |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
304 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
|
305 |
|
97fca735c8da
Teach the random module about os.urandom().
Raymond Hettinger <python@rcn.com>
parents:
32562
diff
changeset
|
306 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
307 class MersenneTwister_TestBasicOps(TestBasicOps): |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
308 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
|
309 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
310 def test_setstate_first_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
311 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
|
312 |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
313 def test_setstate_middle_arg(self): |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
314 # Wrong type, s/b tuple |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
315 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
|
316 # Wrong length, s/b 625 |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
317 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
|
318 # Wrong type, s/b tuple of 625 ints |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
319 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
|
320 # Last element s/b an int also |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
321 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
|
322 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
323 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
|
324 # 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
|
325 # 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
|
326 # 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
|
327 # 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
|
328 # 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
|
329 # 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
|
330 # 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
|
331 # 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
|
332 # 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
|
333 # 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
|
334 # 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
|
335 # 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
|
336 # } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
337 # } |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
338 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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
349 self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
350 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
|
351 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
|
352 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
|
353 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
354 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
|
355 # 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
|
356 # 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
|
357 # 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
|
358 # 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
|
359 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
|
360 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
361 expected = [0x0eab3258d2231fL, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
362 0x1b89db315277a5L, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
363 0x1db622a5518016L, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
364 0x0b7f9af0d575bfL, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
365 0x029e4c4db82240L, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
366 0x04961892f5d673L, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
367 0x02b291598e4589L, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
368 0x11388382c15694L, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
369 0x02dad977c9e1feL, |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
370 0x191d96d4d334c6L] |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
371 self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
372 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
|
373 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
|
374 self.assertEqual(long(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
|
375 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
376 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
|
377 # 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
|
378 # 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
|
379 # 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
|
380 # 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
|
381 # so don't make this horribly big. |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
382 seed = (1L << (10000 * 8)) - 1 # about 10K bytes |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
383 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
|
384 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
385 def test_53_bits_per_float(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
386 # 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
|
387 span = 2 ** 53 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
388 cum = 0 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
389 for i in xrange(100): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
390 cum |= int(self.gen.random() * span) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
391 self.assertEqual(cum, span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
392 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
393 def test_bigrand(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
394 # 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
|
395 # 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
|
396 span = 2 ** 500 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
397 cum = 0 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
398 for i in xrange(100): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
399 r = self.gen.randrange(span) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
400 self.assertTrue(0 <= r < span) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
401 cum |= r |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
402 self.assertEqual(cum, span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
403 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
404 def test_bigrand_ranges(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
405 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
|
406 start = self.gen.randrange(2 ** i) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
407 stop = self.gen.randrange(2 ** (i-2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
408 if stop <= start: |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
409 return |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
410 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
|
411 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
412 def test_rangelimits(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
413 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
|
414 self.assertEqual(set(range(start,stop)), |
|
54ff68e973ee
* Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
30649
diff
changeset
|
415 set([self.gen.randrange(start,stop) for i in xrange(100)])) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
416 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
417 def test_genrandbits(self): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
418 # Verify cross-platform repeatability |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
419 self.gen.seed(1234567) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
420 self.assertEqual(self.gen.getrandbits(100), |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
421 97904845777343510404718956115L) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
422 # Verify ranges |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
423 for k in xrange(1, 1000): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
424 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
|
425 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
426 # Verify all bits active |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
427 getbits = self.gen.getrandbits |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
428 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
|
429 cum = 0 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
430 for i in xrange(100): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
431 cum |= getbits(span) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
432 self.assertEqual(cum, 2**span-1) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
433 |
|
32562
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
434 # Verify argument checking |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
435 self.assertRaises(TypeError, self.gen.getrandbits) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
436 self.assertRaises(TypeError, self.gen.getrandbits, 'a') |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
437 self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
438 self.assertRaises(ValueError, self.gen.getrandbits, 0) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
439 self.assertRaises(ValueError, self.gen.getrandbits, -1) |
|
a160b9669db1
Add some tests for corner cases.
Raymond Hettinger <python@rcn.com>
parents:
30929
diff
changeset
|
440 |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
441 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
|
442 # 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
|
443 # 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
|
444 # is equal to or one greater than the number of bits in n |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
445 for i in xrange(1, 1000): |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
446 n = 1L << i # check an exact power of two |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
447 numbits = i+1 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
448 k = int(1.00001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
449 self.assertEqual(k, numbits) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
450 self.assertTrue(n == 2**(k-1)) |
|
30649
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
451 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
452 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
|
453 k = int(1.00001 + _log(n, 2)) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
454 self.assertIn(k, [numbits, numbits+1]) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
455 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
|
456 |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
457 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
|
458 k = int(1.00001 + _log(n, 2)) |
|
8841d34a2c37
SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents:
30346
diff
changeset
|
459 self.assertEqual(k, numbits) # note the stronger assertion |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
460 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
|
461 |
|
40489
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
462 def test_randrange_bug_1590891(self): |
|
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
463 start = 1000000000000 |
|
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
464 stop = -100000000000000000000 |
|
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
465 step = -200 |
|
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
466 x = self.gen.randrange(start, stop, step) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
467 self.assertTrue(stop < x <= start) |
|
40489
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
468 self.assertEqual((x+stop)%step, 0) |
|
fe9b56990894
Bug #1590891: random.randrange don't return correct value for big number
Raymond Hettinger <python@rcn.com>
parents:
37360
diff
changeset
|
469 |
|
51737
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
470 def gamma(z, sqrt2pi=(2.0*pi)**0.5): |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
471 # Reflection to right half of complex plane |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
472 if z < 0.5: |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
473 return pi / sin(pi*z) / gamma(1.0-z) |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
474 # Lanczos approximation with g=7 |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
475 az = z + (7.0 - 0.5) |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
476 return az ** (z-0.5) / exp(az) * sqrt2pi * fsum([ |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
477 0.9999999999995183, |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
478 676.5203681218835 / z, |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
479 -1259.139216722289 / (z+1.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
480 771.3234287757674 / (z+2.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
481 -176.6150291498386 / (z+3.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
482 12.50734324009056 / (z+4.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
483 -0.1385710331296526 / (z+5.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
484 0.9934937113930748e-05 / (z+6.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
485 0.1659470187408462e-06 / (z+7.0), |
|
00f7be02ae1f
Inline coefficients in gamma(). Add reflection formula. Add comments.
Raymond Hettinger <python@rcn.com>
parents:
49099
diff
changeset
|
486 ]) |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
487 |
| 27092 | 488 class TestDistributions(unittest.TestCase): |
| 489 def test_zeroinputs(self): | |
| 490 # Verify that distributions can handle a series of zero inputs' | |
| 491 g = random.Random() | |
| 492 x = [g.random() for i in xrange(50)] + [0.0]*5 | |
| 493 g.random = x[:].pop; g.uniform(1,10) | |
| 494 g.random = x[:].pop; g.paretovariate(1.0) | |
| 495 g.random = x[:].pop; g.expovariate(1.0) | |
| 496 g.random = x[:].pop; g.weibullvariate(1.0, 1.0) | |
| 497 g.random = x[:].pop; g.normalvariate(0.0, 1.0) | |
| 498 g.random = x[:].pop; g.gauss(0.0, 1.0) | |
| 499 g.random = x[:].pop; g.lognormvariate(0.0, 1.0) | |
| 500 g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) | |
| 501 g.random = x[:].pop; g.gammavariate(0.01, 1.0) | |
| 502 g.random = x[:].pop; g.gammavariate(1.0, 1.0) | |
| 503 g.random = x[:].pop; g.gammavariate(200.0, 1.0) | |
| 504 g.random = x[:].pop; g.betavariate(3.0, 3.0) | |
|
46029
f1e51785947f
Issue 1681432: Add triangular distribution the random module.
Raymond Hettinger <python@rcn.com>
parents:
44122
diff
changeset
|
505 g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0) |
| 27092 | 506 |
|
27094
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
507 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
|
508 # 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
|
509 # 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
|
510 g = random.Random() |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
511 N = 5000 |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
512 x = [i/float(N) for i in xrange(1,N)] |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
513 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
|
514 (g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12), |
|
46029
f1e51785947f
Issue 1681432: Add triangular distribution the random module.
Raymond Hettinger <python@rcn.com>
parents:
44122
diff
changeset
|
515 (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
|
516 (g.expovariate, (1.5,), 1/1.5, 1/1.5**2), |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
517 (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
|
518 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
|
519 (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
|
520 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
|
521 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
|
522 y = [] |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
523 for i in xrange(len(x)): |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
524 try: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
525 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
|
526 except IndexError: |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
527 pass |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
528 s1 = s2 = 0 |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
529 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
|
530 s1 += e |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
531 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
|
532 N = len(y) |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
533 self.assertAlmostEqual(s1/N, mu, 2) |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
534 self.assertAlmostEqual(s2/(N-1), sigmasqrd, 2) |
|
e841c873a937
Move the statistical tests for four distributions into the unittest suite.
Raymond Hettinger <python@rcn.com>
parents:
27092
diff
changeset
|
535 |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
536 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 |
|
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
543 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
|
544 # tests validity but not completeness of the __all__ list |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51737
diff
changeset
|
545 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
|
546 |
|
40683
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
547 def test_random_subclass_with_kwargs(self): |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
548 # SF bug #1486663 -- this used to erroneously raise a TypeError |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
549 class Subclass(random.Random): |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
550 def __init__(self, newarg=None): |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
551 random.Random.__init__(self) |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
552 Subclass(newarg=1) |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
553 |
|
c86df311e62c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40489
diff
changeset
|
554 |
| 27668 | 555 def test_main(verbose=None): |
|
33406
483c0b4b6332
SF bug #1022010: Import random fails
Raymond Hettinger <python@rcn.com>
parents:
33329
diff
changeset
|
556 testclasses = [WichmannHill_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
|
557 MersenneTwister_TestBasicOps, |
| 27092 | 558 TestDistributions, |
|
33406
483c0b4b6332
SF bug #1022010: Import random fails
Raymond Hettinger <python@rcn.com>
parents:
33329
diff
changeset
|
559 TestModule] |
|
483c0b4b6332
SF bug #1022010: Import random fails
Raymond Hettinger <python@rcn.com>
parents:
33329
diff
changeset
|
560 |
|
33409
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33406
diff
changeset
|
561 try: |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
562 random.SystemRandom().random() |
|
33409
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33406
diff
changeset
|
563 except NotImplementedError: |
|
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33406
diff
changeset
|
564 pass |
|
8e23bd3d5b1a
Fulfill Martin's request to use try/except rather than a "look before
Raymond Hettinger <python@rcn.com>
parents:
33406
diff
changeset
|
565 else: |
|
33481
eacdc29606d8
SF #1027105: HardwareRandom should be renamed OSRandom
Raymond Hettinger <python@rcn.com>
parents:
33409
diff
changeset
|
566 testclasses.append(SystemRandom_TestBasicOps) |
|
33406
483c0b4b6332
SF bug #1022010: Import random fails
Raymond Hettinger <python@rcn.com>
parents:
33329
diff
changeset
|
567 |
|
28944
faf2cb5070b4
Apply the simplified test_support boilerplate.
Raymond Hettinger <python@rcn.com>
parents:
28937
diff
changeset
|
568 test_support.run_unittest(*testclasses) |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
569 |
| 27668 | 570 # verify reference counting |
| 571 import sys | |
| 572 if verbose and hasattr(sys, "gettotalrefcount"): | |
|
28937
c5f18db1c966
Simplify ref count test.
Raymond Hettinger <python@rcn.com>
parents:
27996
diff
changeset
|
573 counts = [None] * 5 |
|
c5f18db1c966
Simplify ref count test.
Raymond Hettinger <python@rcn.com>
parents:
27996
diff
changeset
|
574 for i in xrange(len(counts)): |
|
28944
faf2cb5070b4
Apply the simplified test_support boilerplate.
Raymond Hettinger <python@rcn.com>
parents:
28937
diff
changeset
|
575 test_support.run_unittest(*testclasses) |
|
28937
c5f18db1c966
Simplify ref count test.
Raymond Hettinger <python@rcn.com>
parents:
27996
diff
changeset
|
576 counts[i] = sys.gettotalrefcount() |
| 27668 | 577 print counts |
| 578 | |
|
26829
f64c96922708
SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
579 if __name__ == "__main__": |
| 27668 | 580 test_main(verbose=True) |
