-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs.py
executable file
·29 lines (22 loc) · 786 Bytes
/
pairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
import random
import string
import sys
import math
def pairs(numpairs, r=random.SystemRandom(), useCaps=True):
vowels = set('aeiou')
cons = set(string.ascii_lowercase) - vowels
# r.choice requires an indexable data structure
vowels = list(vowels)
cons = list(cons)
rand_pair = lambda: (r.choice(cons) + r.choice(vowels))
pw = [rand_pair() for i in range(numpairs)]
if useCaps:
pw = map(lambda x: r.choice([x, x.upper()]), pw)
pw = ''.join(pw)
entropy = math.log((len(cons)*len(vowels)*(useCaps and 4 or 1))**numpairs)/math.log(2)
return (pw, entropy)
if __name__ == '__main__':
(pw, entropy) = pairs(int(sys.argv[1]), useCaps=(sys.argv[2]=='t'))
print(pw)
print('entropy: %f bits' % entropy)