7

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...

scherlock
  • 217
  • 3
  • 8
  • 2
    This might help: http://stackoverflow.com/questions/191335/windows-equivalent-of-dev-random – pcalcao Jun 06 '12 at 11:51
  • Thank you for the quick response! I found that one already but I would like to find a solution that would keep the code usable on linux. – scherlock Jun 06 '12 at 11:53

4 Answers4

13

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.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
7

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?

Soz
  • 957
  • 1
  • 5
  • 9
1

You could use random from Python's standard library.

Steve
  • 6,618
  • 3
  • 44
  • 42
  • 2
    -1. From the linked page: "*The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.*" So it is not a substitute for /dev/random (which is cryptographically random.) – finnw Jun 06 '12 at 12:31
  • 1
    OP didn't say anything about cryptographic purposes, and random.SystemRandom (which is in random.*) uses os.urandom which is suitable for crypto. – Steve Jun 06 '12 at 13:21
0

/dev/random is available if you install Cygwin. In the Cygwin console window you'll be able to run the command:

tom@myHost ~
$ hexdump -C -n 8 /dev/random
00000000  4f 9d 57 cc 9a 01 aa cb                           |O.W.....|
00000008

And assuming you add the Cygwin's bin directory to your path, you can also run that command in the Window Command Prompt window, but it'll only work using Cygwin commands.

Microsoft Windows [Version 10.0.18363.778]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\tom>hexdump -C -n 8 /dev/random
00000000  01 99 93 1d 51 f0 dd f4                           |....Q...|
00000008

Don't know if this helps you or not! I don't think you'll be able to use it as device file that you can read from inside an application without some kind of trick such as: see https://www.codeproject.com/articles/66235/interacting-with-shell-applications-made-easy

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15
  • It just occurred to me, you could do something like this: >cat /dev/random | yourApp. I tried it using the dos command "more" as a substitute for "yourApp" and it worked fine! Don't know if there are other issues you'll run into when entropy is empty and the random bits are blocked. But it does seem plausible that could work! – Tom Rutchik Apr 30 '20 at 20:40