This question already has answers here:
Retrieve RNG seed in julia

(6个答案)


4年前关闭。




假设我用srand(123)播种123,并运行rand() X次。以后,我希望能够重新启动Julia并为数字(或状态)加上种子,这样当我再次运行rand()时,我得到的数字是如果我有123种子并运行rand() X + 1次。有什么办法可以做到?还是我真的必须运行rand() X次才能获得我想要的状态?

最佳答案

如果Retrieve RNG seed in julia中提供的使用自定义随机数生成器的解决方案对您不可行,那么我能想到的最好的办法就是复制全局随机数生成器的整个结构:

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()交互。

关于random - 有没有一种方法可以获取随机数生成器的状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43197489/

10-12 18:24