有没有一种方法可以获取随机数生成器的状态? [英] Is there a way to obtain the state of the random number generator?
问题描述
说我用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?
推荐答案
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.
这篇关于有没有一种方法可以获取随机数生成器的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!