admin管理员组

文章数量:1122832

Running Python 3.12, I'm doing lots of reps of an algorithm that uses random numbers. Ultimately, I'd like to record the state of the PRNG at the beginning of each run so that there's some hope of recreating the results (assuming the same version of python, etc.) I also want to use JSON, if only to begin learning that tool.

Here's an MWE:

import random
import json

if __name__ == '__main__':
    random.seed()
    random_state = random.getstate()
    json_state = json.dumps(random_state)
    restored_state = json.loads(json_state)
    random.setstate(restored_state)

The result (in PyCharm 2024.2.3 (Community Edition)) is

Traceback (most recent call last):
  File "/home/paul/PycharmProjects/mwe/main.py", line 10, in <module>
    random.setstate(restored_state)
  File "/usr/lib/python3.12/random.py", line 182, in setstate
    super().setstate(internalstate)
TypeError: state vector must be a tuple

Process finished with exit code 1

I've tried the (trivial) replacement of restored_state in the call to random.setstate by tuple(restored_state), but that results in the same error.

Running Python 3.12, I'm doing lots of reps of an algorithm that uses random numbers. Ultimately, I'd like to record the state of the PRNG at the beginning of each run so that there's some hope of recreating the results (assuming the same version of python, etc.) I also want to use JSON, if only to begin learning that tool.

Here's an MWE:

import random
import json

if __name__ == '__main__':
    random.seed()
    random_state = random.getstate()
    json_state = json.dumps(random_state)
    restored_state = json.loads(json_state)
    random.setstate(restored_state)

The result (in PyCharm 2024.2.3 (Community Edition)) is

Traceback (most recent call last):
  File "/home/paul/PycharmProjects/mwe/main.py", line 10, in <module>
    random.setstate(restored_state)
  File "/usr/lib/python3.12/random.py", line 182, in setstate
    super().setstate(internalstate)
TypeError: state vector must be a tuple

Process finished with exit code 1

I've tried the (trivial) replacement of restored_state in the call to random.setstate by tuple(restored_state), but that results in the same error.

Share Improve this question asked Nov 22, 2024 at 18:48 PaulTanenbaumPaulTanenbaum 1111 silver badge3 bronze badges 3
  • 2 Can you not simply choose a seed and record it instead? – mkrieger1 Commented Nov 22, 2024 at 18:51
  • I have done what @mkrieger1 suggests many times. See also Best practices for generating a random seeds to seed Pytorch? – JonSG Commented Nov 22, 2024 at 20:19
  • The problem with saving the seed is that I may need to reproduce (at some later date) what amount to individual passes through a loop. So my idea is that when I write out each pass's results, I annotate them with the state of the PRNG from when that pass began. – PaulTanenbaum Commented Nov 22, 2024 at 20:40
Add a comment  | 

1 Answer 1

Reset to default 2

json_state is a list but random.setstate() expects a tuple

import random
import json

if __name__ == '__main__':
    random.seed()
    random_state = random.getstate()

    json_state = json.dumps(random_state)
    restored_state_j = json.loads(json_state)

    restored_state = (restored_state_j[0], tuple(restored_state_j[1]), restored_state_j[2])
    random.setstate(restored_state)

本文标签: How to serialize the state of Python39s PRNG using JSONStack Overflow