Random 为什么MersenneTwister似乎为同一种子生成不同的序列?
执行:Random 为什么MersenneTwister似乎为同一种子生成不同的序列?,random,julia,random-seed,Random,Julia,Random Seed,执行: using Random rng1 = MersenneTwister(42) rng2 = MersenneTwister(42) v = [ x for _ = 1:4, x = rand(rng1) ] w = [ rand(rng2) for _ = 1:4 ] print("$(v)\n$(w)\n") 生成以下输出: [0.533183,0.533183,0.533183,0.533183] [0.533183,0.454029,0.0176868,0.172933]
using Random
rng1 = MersenneTwister(42)
rng2 = MersenneTwister(42)
v = [ x for _ = 1:4, x = rand(rng1) ]
w = [ rand(rng2) for _ = 1:4 ]
print("$(v)\n$(w)\n")
生成以下输出:
[0.533183,0.533183,0.533183,0.533183][0.533183,0.454029,0.0176868,0.172933] 这两个序列不应该是相同的吗?我错过了什么
我在多个系统上得到了相同的结果,不管我是先创建
v
还是w
,正如您所看到的,v的所有元素都是相同的。用于构造v的列表仅计算rand(rng1)
一次,然后复制相同的值4次
问题不在于随机数生成器,而在于列表理解