I did a quick search and the only relevant questions I found talk about the old numpy.random interface. I am trying to understand how to use the new interface. I would like to be able to run some simulation for a given amount of time. Then I want to store the random number generator state information to a file so that I can continue the simulation at a later time.
I have found one way to accomplish this, but it seems to me to be a bad idea since it isn't documented in the API anywhere. I'm wondering if there is a simple way that I have somehow overlooked.
Let's say that I start a simulation with the following code.
from numpy.random import Generator, PCG64
rg = Generator(PCG64(12345))
rg.standard_normal(1024)
save_to_file('state.txt', rg.bit_generator.state)
print(rg.standard_normal(8))
Here, save_to_file
saves the dictionary returned by rg.bit_generator.state
to state.txt
. Now, if I want to continue processing the simulation where I saved it at a later time, I can do so by using the following.
from numpy.random import Generator, PCG64
rg = Generator(PCG64())
rg.bit_generator.state = load_from_file('state.txt')
print(rg.standard_normal(8))
This works, the same 8 numbers are printed for me. I figured out how to do this by inspecting the bit_generator object in the python console. I am using Python 3.6.8 and Numpy 1.18.4. The documentation here and here on the bit_generator object is extremely sparse and doesn't have any suggestions for this common (at least in my work) scenario.
This answer to a similar question about the older interface seems to suggest that it is quite difficult to obtain this for the Mersenne Twister (MT19937), but I am using the PCG64 algorithm which seems not to have as much internal state. At least based on the success of the code I have provided. Is there a better way to accomplish this? One that is either documented or condoned by the community at large? Something that won't break without being well-documented if I one day decide to update Numpy.