I read in a book that /dev/random
is like an infinite file, but when I set up the following codes to see what the content look like, it prints nothing.
with open("/dev/random") as f:
for i in xrange(10):
print f.readline()
BTW, when I tried this with /dev/urandom
, it worked.
urandom() method is used to generate a string of size random bytes suitable for cryptographic use or we can say this method generates a string containing random characters. Return Value: This method returns a string which represents random bytes suitable for cryptographic use.
The /dev/random device is intended to provide high quality, cryptographically secure random output and will only return output for which sufficient (an equal or greater amount) random input is available to generate the output.
Using /dev/random may require waiting for the result as it uses so-called entropy pool, where random data may not be available at the moment. /dev/urandom returns as many bytes as user requested and thus it is less random than /dev/random .
In those older kernels, /dev/random would block because the blocking entropy pool had been depleted. However, changes to the algorithms used in ealier kernels meant that there was no longer any useful distinction between the blocking and non-blocking entropy pools.
FWIW, the preferred way of accessing this stream (or something like it) in a semi-portable way is os.urandom()
It is outputting random bytes, not random lines. You see nothing until you get a newline, which will only happen every 256 bytes on average. The reason /dev/urandom
appears to work is simply that it operates faster. Wait longer, read less, or use /dev/urandom
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With