當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Julia Random.seed!用法及代碼示例

用法:

seed!([rng=GLOBAL_RNG], seed) -> rng
seed!([rng=GLOBAL_RNG]) -> rng

重新設定隨機數生成器的種子:rng 當且僅當提供 seed 時才會給出可重現的數字序列。一些 RNG 不接受種子,例如 RandomDevice 。調用 seed! 後,rng 相當於使用相同種子初始化的新創建對象。

如果未指定rng,則默認為共享task-local 生成器的狀態播種。

例子

julia> Random.seed!(1234);

julia> x1 = rand(2)
2-element Array{Float64,1}:
 0.590845
 0.766797

julia> Random.seed!(1234);

julia> x2 = rand(2)
2-element Array{Float64,1}:
 0.590845
 0.766797

julia> x1 == x2
true

julia> rng = MersenneTwister(1234); rand(rng, 2) == x1
true

julia> MersenneTwister(1) == Random.seed!(rng, 1)
true

julia> rand(Random.seed!(rng), Bool) # not reproducible
true

julia> rand(Random.seed!(rng), Bool)
false

julia> rand(MersenneTwister(), Bool) # not reproducible either
true

相關用法


注:本文由純淨天空篩選整理自julialang.org 大神的英文原創作品 Random.seed! — Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。