summaryrefslogtreecommitdiff
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.py38
1 files changed, 32 insertions, 6 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index df07b76c0e..776d0c4163 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