2

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.

hops
  • 221
  • 2
  • 11

1 Answers1

2

Accessing the bit generator through rg is the same as declaring pg = PCG64() and then accessing pg.state. There's nothing wrong with accessing via rg.bit_generator. The docs are a bit scarce but the docs for BitGenerator state that accessing BitGenerator.state allows you to get and set the state if you chose BitGenerator.

https://numpy.org/doc/stable/reference/random/bit_generators/generated/numpy.random.PCG64.state.html?highlight=pcg64

Trenton McKinney
  • 29,033
  • 18
  • 54
  • 66
Harben
  • 1,430
  • 1
  • 9
  • 16
  • So, this seems to indicate that what I'm doing is perhaps the correct way to go then? – hops Jul 24 '20 at 20:52
  • Yeah you're just accessing the BitGenerator via your `rg` variable instead of a declared PCG64 variable.You're not accessing any private or hidden variables and the docs point to this being the correct way to do so. – Harben Jul 24 '20 at 21:23