Is there a Python library that can produce random data as fast as os.urandom, but the data can be reproduced if given a seed?
Asked
Active
Viewed 392 times
-4
-
How did your search on "Python random seed" not turn up the hits? – Prune Apr 02 '18 at 18:21
-
You do know the difference between what `os.urandom` does and pseudo-random generators do, right? – Jon Clements Apr 02 '18 at 18:22
-
The speeds given by `pv < /dev/urandom > /dev/null` vary significantly between systems. Mine cluster around ~13MiB/s, with one producing ~180MiB/s. These aren't particularly fast rates. What are you going to be using this random data for? – Blender Apr 02 '18 at 18:30
-
I know that os.urandom is totally random, but I could not find a suitable pseudo-random generator that did a similar thing. I'm using the data to test write functionality to a bunch of luns. – Laharl Apr 02 '18 at 18:53
1 Answers
1
You can use random.seed
to produce a reproducible sequence. The only problem is getting Python to produce random bytes quickly. You can use a trick observed by @jfs with random.getrandbits
to reduce the amount of processing Python has to do:
import random
def almost_urandom(n):
return random.getrandbits(8 * n).to_bytes(n, 'big')
random.seed
lets you deterministically generate the bytes:
In [26]: random.seed(0)
In [27]: almost_urandom(10)
Out[27]: b'\xc2\tb\x9fo\xbe\xd8,\x07\xcd'
In [28]: almost_urandom(10)
Out[28]: b'\n]k\xaa\x94U\xe3\xe7\x06\x82'
In [29]: random.seed(0)
In [30]: almost_urandom(10)
Out[30]: b'\xc2\tb\x9fo\xbe\xd8,\x07\xcd'
In [31]: almost_urandom(10)
Out[31]: b'\n]k\xaa\x94U\xe3\xe7\x06\x82'
It runs an order of magnitude faster than os.urandom()
for me, even for n
in the millions.

Blender
- 289,723
- 53
- 439
- 496