diff options
author | Thorsten Behrens <sbehrens@gmx.li> | 2011-01-05 07:54:04 -0500 |
---|---|---|
committer | Thorsten Behrens <sbehrens@gmx.li> | 2011-01-05 07:54:04 -0500 |
commit | b27696462b1e7c6c53ce2ac6760567eb6ff744b6 (patch) | |
tree | 83e2519ba30f81225c6c417f0be721926c5b04b2 | |
parent | ff0e5ad4093ccbb3f1935dd7753515c42c0484e5 (diff) | |
download | pycrypto-b27696462b1e7c6c53ce2ac6760567eb6ff744b6.tar.gz |
Improve random selftest
o Random selftest is improved, less likely to collide
o random.shuffle() is more pythonic
-rw-r--r-- | lib/Crypto/Random/random.py | 4 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/Random/test_random.py | 7 |
2 files changed, 4 insertions, 7 deletions
diff --git a/lib/Crypto/Random/random.py b/lib/Crypto/Random/random.py index bc08889..9d969f2 100644 --- a/lib/Crypto/Random/random.py +++ b/lib/Crypto/Random/random.py @@ -109,9 +109,7 @@ class StrongRandom(object): # Choose a random item (without replacement) until all the items have been # chosen. for i in xrange(len(x)): - p = self.randint(1,len(items)) - x[i] = items[p-1] - del items[p-1] + x[i] = items.pop(self.randrange(len(items))) def sample(self, population, k): """Return a k-length list of unique elements chosen from the population sequence.""" diff --git a/lib/Crypto/SelfTest/Random/test_random.py b/lib/Crypto/SelfTest/Random/test_random.py index d25756f..f9ffc66 100644 --- a/lib/Crypto/SelfTest/Random/test_random.py +++ b/lib/Crypto/SelfTest/Random/test_random.py @@ -95,9 +95,7 @@ class SimpleTest(unittest.TestCase): self.assertRaises(TypeError, random.randint, "1", stop) self.assertRaises(TypeError, random.randint, 1, "2") # Test choice - seq = [] - for i in range(500): # seed the sequence - seq[i:] = [random.getrandbits(32)] + seq = range(10000) x = random.choice(seq) y = random.choice(seq) self.assertNotEqual(x, y) @@ -105,7 +103,7 @@ class SimpleTest(unittest.TestCase): self.assertEqual(y in seq, True) for i in range(10): self.assertEqual(random.choice((1,2,3)) in (1,2,3), True) - self.assertEqual(random.choice(range(3)) in range(3), True) + self.assertEqual(random.choice([1,2,3]) in [1,2,3], True) if sys.version_info[0] is 3: self.assertEqual(random.choice(bytearray(b('123'))) in bytearray(b('123')), True) self.assertEqual(1, random.choice([1])) @@ -113,6 +111,7 @@ class SimpleTest(unittest.TestCase): self.assertRaises(TypeError, random.choice, 1) # Test shuffle. Lacks random parameter to specify function. # Make copies of seq + seq = range(500) x = list(seq) y = list(seq) random.shuffle(x) |