Reputation: 9373
I am asked:
Using your raspberry pi, write a python script that determines the randomness of /dev/random and /dev/urandom. Read bytes and histogram the results. Plot in matplotlib. For your answer include the python script.
I am currently lost on the phrasing "determines the randomness."
I can read from urandom and random with:
#rb - reading as binary
devrndm = open("/dev/random", 'rb')
#read to a file instead of mem?
rndmdata = devrndm.read(25) #read 25bytes
or
with open("/dev/random", 'rb') as f:
print repr(f.read(10))
I think the purpose of this exercise was to find out that urandom is faster and has a larger pool than random. However if I try to read anything over ~15 the time to read seems to increase exponentially.
So I am lost right now on how to compare 'randomness'. If I read both urandom and random in to respective files how could I compare them?
Upvotes: 0
Views: 713
Reputation: 4903
Could it be as simple as:
In [664]: f = open("/dev/random", "rb")
In [665]: len(set(f.read(256)))
Out[665]: 169
In [666]: ff = open("/dev/urandom", "rb")
In [667]: len(set(ff.read(256)))
Out[667]: 167
In [669]: len(set(f.read(512)))
Out[669]: 218
In [670]: len(set(ff.read(512)))
Out[670]: 224
ie. asking for 256 bytes doesn't give back 256 unique values. So you could plot increasing sample sizes against the unique count until it reaches the 256 saturation point.
Upvotes: 2
Reputation: 27232
Your experience may be exactly what they're looking for. From the man page of urandom(4):
When read, the /dev/random device will only return random bytes within the estimated number of bits of noise in the entropy pool. /dev/random should be suitable for uses that need very high quality randomness such as one-time pad or key generation. When the entropy pool is empty, reads from /dev/random will block until addi‐ tional environmental noise is gathered.
A read from the /dev/urandom device will not block waiting for more entropy.
Note the bit about blocking. urandom won't, random will. Particularly in an embedded context, additional entropy may be hard to come by, which would lead to the blocking you see.
Upvotes: 3