简体   繁体   English

在R和Julia中生成相同的随机数

[英]Generate identical random numbers in R and Julia

I'd like to generate identical random numbers in R and Julia. 我想在R和Julia中生成相同的随机数。 Both languages appear to use the Mersenne-Twister library by default, however in Julia 1.0.0: 默认情况下,这两种语言似乎都使用Mersenne-Twister库,但在Julia 1.0.0中:

julia> using Random
julia> Random.seed!(3)
julia> rand()
0.8116984049958615

Produces 0.811... , while in R: 产生0.811... ,而在R:

set.seed(3)
runif(1)

produces 0.168 . 产生0.168

Any ideas? 有任何想法吗?

Related SO questions here and here . 相关的SO问题在这里这里

My use case for those who are interested : Testing new Julia code that requires random number generation (eg statistical bootstrapping) by comparing output to that from equivalent libraries in R. 我感兴趣的用例 :通过比较输出与R中等效库的输出,测试需要随机数生成(例如统计引导)的新Julia代码

That is an old problem. 这是一个老问题。

Paul Gilbert addressed the same issue in the late 1990s (!!) when trying to assert that simulations in R (then then newcomer) gave the same result as those in S-Plus (then the incumbent). 保罗吉尔伯特在20世纪90年代后期(!!)试图断言R中的模拟(然后是新手)给出的结果与S-Plus(然后是现任者)的结果相同时解决了同样的问题。

His solution, and still the golden approach AFAICT: re-implement in fresh code in both languages as the this the only way to ensure identical seeding, state, ... and whatever else affects it. 他的解决方案,仍然是AFAICT的黄金方法:用两种语言的新代码重新实现,因为这是确保相同种子,状态,......以及其他任何影响它的唯一方法。

Pursuing the RCall suggestion made by @Khashaa, it's clear that you can set the seed and get the random numbers from R . 追求RCall提出的RCall建议,很明显你可以设置种子并从R获取随机数。

julia> using RCall

julia> RCall.reval("set.seed(3)")
RCall.NilSxp(16777344,Ptr{Void} @0x0a4b6330)

julia> a = zeros(Float64,20);

julia> unsafe_copy!(pointer(a), RCall.reval("runif(20)").pv, 20)
Ptr{Float64} @0x972f4860

julia> map(x -> @printf("%20.15f\n", x), a);
   0.168041526339948
   0.807516399072483
   0.384942351374775
   0.327734317164868
   0.602100674761459
   0.604394054040313
   0.124633444240317
   0.294600924244151
   0.577609919011593
   0.630979274399579
   0.512015897547826
   0.505023914156482
   0.534035353455693
   0.557249435689300
   0.867919487645850
   0.829708693316206
   0.111449153395370
   0.703688358888030
   0.897488264366984
   0.279732553754002

and from R : 来自R

> options(digits=15)
> set.seed(3)
> runif(20)
 [1] 0.168041526339948 0.807516399072483 0.384942351374775 0.327734317164868
 [5] 0.602100674761459 0.604394054040313 0.124633444240317 0.294600924244151
 [9] 0.577609919011593 0.630979274399579 0.512015897547826 0.505023914156482
[13] 0.534035353455693 0.557249435689300 0.867919487645850 0.829708693316206
[17] 0.111449153395370 0.703688358888030 0.897488264366984 0.279732553754002

** EDIT ** ** 编辑 **

Per the suggestion by @ColinTBowers, here's a simpler/cleaner way to access R random numbers from Julia . 根据@ColinTBowers的建议,这里有一个更简单/更清晰的方式从Julia访问R随机数。

julia> using RCall

julia> reval("set.seed(3)");

julia> a = rcopy("runif(20)");

julia> map(x -> @printf("%20.15f\n", x), a);
   0.168041526339948
   0.807516399072483
   0.384942351374775
   0.327734317164868
   0.602100674761459
   0.604394054040313
   0.124633444240317
   0.294600924244151
   0.577609919011593
   0.630979274399579
   0.512015897547826
   0.505023914156482
   0.534035353455693
   0.557249435689300
   0.867919487645850
   0.829708693316206
   0.111449153395370
   0.703688358888030
   0.897488264366984
   0.279732553754002

See: 看到:

?set.seed

"Mersenne-Twister": From Matsumoto and Nishimura (1998). “Mersenne-Twister”:来自Matsumoto和Nishimura(1998)。 A twisted GFSR with period 2^19937 - 1 and equidistribution in 623 consecutive dimensions (over the whole period). 扭曲的GFSR,周期为2 ^ 19937 - 1,等分布在623个连续维度(整个期间)。 The 'seed' is a 624-dimensional set of 32-bit integers plus a current position in that set. '种子'是一组624维的32位整数加上该组中的当前位置。

And you might see if you can link to the same C code from both languages. 您可能会看到是否可以从两种语言链接到相同的C代码。 If you want to see the list/vector, type: 如果要查看列表/向量,请键入:

.Random.seed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM