Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `/dev/urandom` suitable for simulation purpose?

Tags:

c

linux

unix

random

It seems that using plain C in unix-like systems, fread from /dev/urandom is the simplest way to extract high quality random bytes. I need to run a simulation that needs about 10k 32-bit random numbers per second, and it may run for several days. Is /dev/urandom okay to use for this purpose? How is the quality of random bytes from here when the entropy pool is depleted?

edit_1

While I'm now running 3 parallel diehard tests for /dev/urandom in my laptop, I got the following interesting lines. The test isn't yet complete.

#=============================================================================#
        test_name   |ntup| tsamples |psamples|  p-value |Assessment
#=============================================================================#
 diehard_parking_lot|   0|     12000|     100|0.99573896|   WEAK
        diehard_sums|   0|       100|     100|0.00116464|   WEAK
          sts_serial|   7|    100000|     100|0.99996076|   WEAK

1 Answers

In the underlying implementation of /dev/urandom is a CSPRNG, the output pool of which has a maximal period of less than 2^(26∗32) − 1, which is then fed into SHA-1 to produce output for /dev/urandom. As such, urandom can obviously produce the amount of random numbers you want, however it can not offer you reproducible results - you will have to cache the sequence you get yourself.

You do not have to worry about what happens when the entropy pool is estimated to be depleted, /dev/urandom will output whatever you request of it. The "theoretical attacks" the urandom(4) man page speaks of are nonexistent. (the "issue" is a huge misunderstanding of what "entropy estimation" is)

Many other PRNGs with large periods exist which reproducible seeding: the Mersenne Twister in C++, xorshift PRNGs, etc. You should be able to adapt any PRNG to the distribution which is suitable for your purposes.

like image 85
Michael Foukarakis Avatar answered Oct 17 '25 08:10

Michael Foukarakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!