summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_random.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r--Lib/test/test_random.py86
1 files changed, 78 insertions, 8 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index df07b76..97d8098 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -1,11 +1,11 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
import unittest
import random
import time
import pickle
import warnings
-from math import log, exp, sqrt, pi, fsum, sin
+from math import log, exp, pi, fsum, sin
from test import support
class TestBasicOps(unittest.TestCase):
@@ -39,9 +39,16 @@ class TestBasicOps(unittest.TestCase):
self.gen.seed(arg)
for arg in [list(range(3)), dict(one=1)]:
self.assertRaises(TypeError, self.gen.seed, arg)
- self.assertRaises(TypeError, self.gen.seed, 1, 2)
+ self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
self.assertRaises(TypeError, type(self.gen), [])
+ def test_choice(self):
+ choice = self.gen.choice
+ with self.assertRaises(IndexError):
+ choice([])
+ self.assertEqual(choice([50]), 50)
+ self.assertIn(choice([25, 75]), [25, 75])
+
def test_sample(self):
# For the entire allowable range of 0 <= k <= N, validate that
# the sample is of the correct length and contains only unique items
@@ -121,7 +128,15 @@ class TestBasicOps(unittest.TestCase):
f = open(support.findfile(file),"rb")
r = pickle.load(f)
f.close()
- self.assertEqual(r.randrange(1000), value)
+ self.assertEqual(int(r.random()*1000), value)
+
+ def test_bug_9025(self):
+ # Had problem with an uneven distribution in int(n*random())
+ # Verify the fix by checking that distributions fall within expectations.
+ n = 100000
+ randrange = self.gen.randrange
+ k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
+ self.assertTrue(0.30 < k/n < .37, (k/n))
class SystemRandom_TestBasicOps(TestBasicOps):
gen = random.SystemRandom()
@@ -211,7 +226,7 @@ class SystemRandom_TestBasicOps(TestBasicOps):
n += n - 1 # check 1 below the next power of two
k = int(1.00001 + _log(n, 2))
- self.assertTrue(k in [numbits, numbits+1])
+ self.assertIn(k, [numbits, numbits+1])
self.assertTrue(2**k > n > 2**(k-2))
n -= n >> 15 # check a little farther below the next power of two
@@ -223,6 +238,17 @@ class SystemRandom_TestBasicOps(TestBasicOps):
class MersenneTwister_TestBasicOps(TestBasicOps):
gen = random.Random()
+ def test_guaranteed_stable(self):
+ # These sequences are guaranteed to stay the same across versions of python
+ self.gen.seed(3456147, version=1)
+ self.assertEqual([self.gen.random().hex() for i in range(4)],
+ ['0x1.ac362300d90d2p-1', '0x1.9d16f74365005p-1',
+ '0x1.1ebb4352e4c4dp-1', '0x1.1a7422abf9c11p-1'])
+ self.gen.seed("the quick brown fox", version=2)
+ self.assertEqual([self.gen.random().hex() for i in range(4)],
+ ['0x1.1239ddfb11b7cp-3', '0x1.b3cbb5c51b120p-4',
+ '0x1.8c4f55116b60fp-1', '0x1.63eb525174a27p-1'])
+
def test_setstate_first_arg(self):
self.assertRaises(ValueError, self.gen.setstate, (1, None, None))
@@ -367,7 +393,7 @@ class MersenneTwister_TestBasicOps(TestBasicOps):
n += n - 1 # check 1 below the next power of two
k = int(1.00001 + _log(n, 2))
- self.assertTrue(k in [numbits, numbits+1])
+ self.assertIn(k, [numbits, numbits+1])
self.assertTrue(2**k > n > 2**(k-2))
n -= n >> 15 # check a little farther below the next power of two
@@ -410,6 +436,7 @@ class TestDistributions(unittest.TestCase):
g.random = x[:].pop; g.paretovariate(1.0)
g.random = x[:].pop; g.expovariate(1.0)
g.random = x[:].pop; g.weibullvariate(1.0, 1.0)
+ g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0)
g.random = x[:].pop; g.normalvariate(0.0, 1.0)
g.random = x[:].pop; g.gauss(0.0, 1.0)
g.random = x[:].pop; g.lognormvariate(0.0, 1.0)
@@ -430,6 +457,7 @@ class TestDistributions(unittest.TestCase):
(g.uniform, (1.0,10.0), (10.0+1.0)/2, (10.0-1.0)**2/12),
(g.triangular, (0.0, 1.0, 1.0/3.0), 4.0/9.0, 7.0/9.0/18.0),
(g.expovariate, (1.5,), 1/1.5, 1/1.5**2),
+ (g.vonmisesvariate, (1.23, 0), pi, pi**2/3),
(g.paretovariate, (5.0,), 5.0/(5.0-1),
5.0/((5.0-1)**2*(5.0-2))),
(g.weibullvariate, (1.0, 3.0), gamma(1+1/3.0),
@@ -446,8 +474,50 @@ class TestDistributions(unittest.TestCase):
s1 += e
s2 += (e - mu) ** 2
N = len(y)
- self.assertAlmostEqual(s1/N, mu, places=2)
- self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2)
+ self.assertAlmostEqual(s1/N, mu, places=2,
+ msg='%s%r' % (variate.__name__, args))
+ self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2,
+ msg='%s%r' % (variate.__name__, args))
+
+ def test_constant(self):
+ g = random.Random()
+ N = 100
+ for variate, args, expected in [
+ (g.uniform, (10.0, 10.0), 10.0),
+ (g.triangular, (10.0, 10.0), 10.0),
+ #(g.triangular, (10.0, 10.0, 10.0), 10.0),
+ (g.expovariate, (float('inf'),), 0.0),
+ (g.vonmisesvariate, (3.0, float('inf')), 3.0),
+ (g.gauss, (10.0, 0.0), 10.0),
+ (g.lognormvariate, (0.0, 0.0), 1.0),
+ (g.lognormvariate, (-float('inf'), 0.0), 0.0),
+ (g.normalvariate, (10.0, 0.0), 10.0),
+ (g.paretovariate, (float('inf'),), 1.0),
+ (g.weibullvariate, (10.0, float('inf')), 10.0),
+ (g.weibullvariate, (0.0, 10.0), 0.0),
+ ]:
+ for i in range(N):
+ self.assertEqual(variate(*args), expected)
+
+ def test_von_mises_range(self):
+ # Issue 17149: von mises variates were not consistently in the
+ # range [0, 2*PI].
+ g = random.Random()
+ N = 100
+ for mu in 0.0, 0.1, 3.1, 6.2:
+ for kappa in 0.0, 2.3, 500.0:
+ for _ in range(N):
+ sample = g.vonmisesvariate(mu, kappa)
+ self.assertTrue(
+ 0 <= sample <= random.TWOPI,
+ msg=("vonmisesvariate({}, {}) produced a result {} out"
+ " of range [0, 2*pi]").format(mu, kappa, sample))
+
+ def test_von_mises_large_kappa(self):
+ # Issue #17141: vonmisesvariate() was hang for large kappas
+ random.vonmisesvariate(0, 1e15)
+ random.vonmisesvariate(0, 1e100)
+
class TestModule(unittest.TestCase):
def testMagicConstants(self):