I used to generate random string in the following way (now I've switched to this method).
key = '%016x' % random.getrandbits(128)
The key generated this way is most often a 32 character string, but once I've got 31 chars.
This is what I don't get: why it's 32 chars, not 16? Doesn't one hex digit take one character to print?
So if I ask for %016x
- shouldn't one expect sixteen chars with possible leading zeroes?
Why string legth is not always the same?
Test case
import random
import collections
stats = collections.defaultdict(int)
for i in range(1000000):
key = '%016x' % random.getrandbits(128)
length = len(key)
stats[length] += 1
for key in stats:
print key, ' ', stats[key]
Prints:
32 937911
27 1
28 9
29 221
30 3735
31 58123