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.
- 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
1 Answer
Reset to default 2json_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
版权声明:本文标题:How to serialize the state of Python's PRNG using JSON? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301493a1931195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论