I'm trying to port python code from linux to windows right now. In various places random numbers are generateted by reading from /dev/random. Is there a way to simulate /dev/random on Windows?
I'm looking for a solution that would keep the code useable on linux...
/dev/random uses an entropy pool of 4096 bits (512 Bytes) to generate random data and stops when the pool is exhausted until it gets (slowly) refilled. /dev/random is designed for generating cryptographic keys (e.g. SSL, SSH, dm-crypt's LUKS), but it is impractical to use for wiping current HDD capacities: what makes ...
Cygwin on Windows provides implementations of both /dev/random and /dev/urandom, which can be used in scripts and programs.
The /dev/urandom device provides a reliable source of random output, however the output will not be generated from an equal amount of random input if insufficient input is available.
The key difference between /dev/random versus /dev/urandom is whether a threshold of enough entropy has to be reached before random numbers are generated. Reading from /dev/random will be put on hold if the kernel has not gathered enough entropy to provide the requested amount of data.
If you are using Python, why do you care about the specific implementation? Just use the random
module and let it deal with it.
Beyond that, (if you can't rely on software state) os.urandom
provides os-based random values:
On a UNIX-like system this will query /dev/urandom, and on Windows it will use CryptGenRandom.
(Note that random.SystemRandom
provides a nice interface for this).
If you are really serious about it being cryptographically random, you might want to check out PyCrypto.
You could call random.SystemRandom instead. This will use CryptGenRandom on Windows and /dev/urandom on Linux.
Otherwise, there's always Cygwin's /dev/random?
You could use random from Python's standard library.
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