I'm using kubuntu with kernel 2.6.38-12-generic
I want to read 16 random numbers from /dev/random at the start of my program. However, it blocks after a relatively short time.
How long does it take for the /dev/random buffer to fill? why is it taking so long to fill.
I'm using this as a uuid generator with other sources of randomness added to seed my mersenne twister. It's critical that I don't get duplicates or a duplicate seed.
If I change to /dev/urandom it works ok. Any view on using /dev/random over /dev/urandom.
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.
The /dev/urandom device typically was never a blocking device, even if the pseudorandom number generator seed was not fully initialized with entropy since boot. Not all operating systems implement the same methods for /dev/random and /dev/urandom.
Applications should read from /dev/urandom when they need randomly generated data, e.g. cryptographic keys or seeds for simulations.
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.
You really should never use /dev/random
. There are no known circumstances where the advantages of /dev/random
over /dev/urandom
matter, and the disadvantages are pretty obvious.
The difference is that /dev/urandom
provides 'merely' cryptographically-secure random numbers while /dev/random
provides truly random numbers (at least, that is what we believe). But there is no known situation where this difference matters and no known test that can distinguish "true" randomness from merely cryptographically-secure randomness.
I usually joke that /dev/urandom
provides water and /dev/random
provides holy water.
The man page of man 4 random
answers the question:
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 additional environmental noise is gathered.
I'm so surprised people prefer asking than reading the man pages! You don't even need Internet to read the man pages of your system.
BTW, as I commented, the entropy pool is fed by physical phenomena (depends of the hardware), like e.g. mouse movements, key presses, ethernet packets, etc. Some few processors have a hardware random noise generator (e.g. the RDRAND
machine instruction), and you can buy random USB devices (see also this list), etc.... Hence reading from /dev/random
could be expansive (or even blocking). You'll use it for high quality randomness (e.g. required by cryptographic keys) or, at initialization, for seeding your PRNG. You should expect /dev/random
to have a relatively small bandwidth (e.g. a few kilobytes or at most a megabyte per second at most) and it could have a lot of latency (dozens of milliseconds, or even more). Details are of course computer specific.
Read also Thomas Hühn's Myths about /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