summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_random.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-04-17 20:51:28 (GMT)
committerGitHub <noreply@github.com>2020-04-17 20:51:28 (GMT)
commit223221b290db00ca1042c77103efcbc072f29c90 (patch)
tree199bb4c0de90ab39ee03500b83b8c312250895cb /Lib/test/test_random.py
parent5b1d9184bb0e34391637c06bc7651fb6de8a6240 (diff)
downloadcpython-223221b290db00ca1042c77103efcbc072f29c90.zip
cpython-223221b290db00ca1042c77103efcbc072f29c90.tar.gz
cpython-223221b290db00ca1042c77103efcbc072f29c90.tar.bz2
bpo-40286: Makes simpler the relation between randbytes() and getrandbits() (GH-19574)
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r--Lib/test/test_random.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index efac36a..50d4e94 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -758,7 +758,7 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
# Mersenne Twister randbytes() is deterministic
# and does not depend on the endian and bitness.
seed = 8675309
- expected = b'f\xf9\xa836\xd0\xa4\xf4\x82\x9f\x8f\x19\xf0eo\x02'
+ expected = b'3\xa8\xf9f\xf4\xa4\xd06\x19\x8f\x9f\x82\x02oe\xf0'
self.gen.seed(seed)
self.assertEqual(self.gen.randbytes(16), expected)
@@ -773,19 +773,35 @@ class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
self.assertEqual(b''.join([self.gen.randbytes(4) for _ in range(4)]),
expected)
- # Each randbytes(2) or randbytes(3) call consumes 4 bytes of entropy
+ # Each randbytes(1), randbytes(2) or randbytes(3) call consumes
+ # 4 bytes of entropy
self.gen.seed(seed)
- expected2 = b''.join(expected[i:i + 2]
+ expected1 = expected[3::4]
+ self.assertEqual(b''.join(self.gen.randbytes(1) for _ in range(4)),
+ expected1)
+
+ self.gen.seed(seed)
+ expected2 = b''.join(expected[i + 2: i + 4]
for i in range(0, len(expected), 4))
self.assertEqual(b''.join(self.gen.randbytes(2) for _ in range(4)),
expected2)
self.gen.seed(seed)
- expected3 = b''.join(expected[i:i + 3]
+ expected3 = b''.join(expected[i + 1: i + 4]
for i in range(0, len(expected), 4))
self.assertEqual(b''.join(self.gen.randbytes(3) for _ in range(4)),
expected3)
+ def test_randbytes_getrandbits(self):
+ # There is a simple relation between randbytes() and getrandbits()
+ seed = 2849427419
+ gen2 = random.Random()
+ self.gen.seed(seed)
+ gen2.seed(seed)
+ for n in range(9):
+ self.assertEqual(self.gen.randbytes(n),
+ gen2.getrandbits(n * 8).to_bytes(n, 'little'))
+
def gamma(z, sqrt2pi=(2.0*pi)**0.5):
# Reflection to right half of complex plane