问题描述
假设我使用 srand(123)
播种 123,然后运行 rand()
X 次.稍后,我希望能够重新启动 Julia 并播种一个数字(或状态),这样当我再次运行 rand()
时,我会得到如果我有种子 123 并运行会生成的数字rand()
X + 1 次.有什么办法可以做到这一点,还是我真的必须运行 rand()
X 次才能获得我想要的状态?
Say I seed 123 with srand(123)
, and run rand()
X times. Later, I want to be able to restart Julia and seed a number (or state) such that when I run rand()
again I get the number that would have been generated if I had seed 123 and run rand()
X + 1 times. Is there any way I can do that, or do I really have to run rand()
X times to obtain the state I want?
推荐答案
如果在 在julia中检索RNG种子对你来说是不可行的,我能想出的最好办法是复制全局随机数生成器的整个结构:
If the solution with custom random number generator presented in Retrieve RNG seed in julia is not feasible for you the best I can come up with is to copy the whole structure of global random number generator:
function reset_global_rng(rng_state)
Base.Random.GLOBAL_RNG.seed = rng_state.seed
Base.Random.GLOBAL_RNG.state = rng_state.state
Base.Random.GLOBAL_RNG.vals = rng_state.vals
Base.Random.GLOBAL_RNG.idx = rng_state.idx
end
rs = deepcopy(Base.Random.GLOBAL_RNG)
println(rand(5))
# [0.301558,0.602108,0.220952,0.0338732,0.553414]
reset_global_rng(rs)
println(rand(5))
# [0.301558,0.602108,0.220952,0.0338732,0.553414]
虽然我不是 100% 确定它是如何不与 random.jl 中的 dsfmt_gv_srand()
交互的.
although I am not 100% sure how it does not come into interaction with dsfmt_gv_srand()
in random.jl.
这篇关于有没有办法获取随机数生成器的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!