Search code examples
pythonpython-3.xnumpyrandomrandom-seed

Is it possible to change the seed of a random generator in NumPy?


Say I instantiated a random generator with

import numpy as np
rng = np.random.default_rng(seed=42)

and I want to change its seed. Is it possible to update the seed of the generator instead of instantiating a new generator with the new seed?

I managed to find that you can see the state of the generator with rng.__getstate__(), for example in this case it is

{'bit_generator': 'PCG64', 
 'state': {'state': 274674114334540486603088602300644985544, 
 'inc': 332724090758049132448979897138935081983}, 
 'has_uint32': 0, 
 'uinteger': 0}

and you can change it with rng.__setstate__ with arguments as printed above, but it is not clear to me how to set those arguments so that to have the initial state of the rng given a different seed. Is it possible to do so or instantiating a new generator is the only way?


Solution

  • N.B. The other answer (https://stackoverflow.com/a/74474377/2954547) is better. Use that one, not this one.


    This is maybe a silly hack, but one solution is to create a new RNG instance using the desired new seed, then replace the state of the existing RNG instance with the state of the new instance:

    import numpy as np
    
    seed = 12345
    
    rng = np.random.default_rng(seed)
    x1 = rng.normal(size=10)
    
    rng.__setstate__(np.random.default_rng(seed).__getstate__())
    x2 = rng.normal(size=10)
    
    np.testing.assert_array_equal(x1, x2)
    

    However this isn't much different from just replacing the RNG instance.

    Edit: To answer the question more directly, I don't think it's possible to replace the seed without constructing a new Generator or BitGenerator, unless you know how to correctly construct the state data for the particular BitGenerator inside your Generator. Creating a new RNG is fairly cheap, and while I understand the conceptual appeal of not instantiating a new one, the only alternative here is to post a feature request on the Numpy issue tracker or mailing list.