Search code examples
pythonpython-3.xrandom-data

Does Python have a library that produces data as fast os.urandom but is reproducible?


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?


Solution

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