RNGscope()

  • 前の記事でRNGScope()、RNGScope zを試して、それを使わないバージョンも試した
fx <- cxxfunction(signature(),'RNGScope();return rnorm(5, 0, 100);',plugin="Rcpp")
fx2 <- cxxfunction(signature(),'return rnorm(5, 0, 100);',plugin="Rcpp")
fx3 <- cxxfunction(signature(),'RNGScope z;return rnorm(5, 0, 100);',plugin="Rcpp")
  • Rを起動しなおして、RNGScopeを使わない関数をコンパイルしなおすと、複数の乱数を得ることができなくなった
  • RNGScopeがなんだかわかっていないからだが…
  • RNGScopeを使った関数をコンパイルすると、その後は大丈夫…
  • と言うわけで、今のところは、決め打ち乱数列を作るのではなくて、普通にシミュレーションするのであれば、RNGScope zを使っておくことにしよう
> fx.rexp <- cxxfunction(signature(N="integer",M="numeric"),src,plugin="Rcpp")
> fx.rexp(10,3)
 [1] 7.39357 7.39357 7.39357 7.39357 7.39357 7.39357 7.39357
 [8] 7.39357 7.39357 7.39357
> fx.rexp(10,2)
 [1] 11.09035 11.09035 11.09035 11.09035 11.09035 11.09035
 [7] 11.09035 11.09035 11.09035 11.09035
> fx.rexp(3,2)
[1] 11.09035 11.09035 11.09035
> fx3 <- cxxfunction(signature(),'RNGScope z;return rnorm(5, 0, 100);',plugin="Rcpp")
> fx3()
[1]  50.903500 108.622993 109.351481  -8.016455 -71.923419
> fx3()
[1] -138.281794 -103.148311   16.442448    9.245157   17.948599
> fx.rexp(3,2)
[1] 1.76888569 0.06728493 0.03673216
> fx.rexp(3,2)
[1] 0.2125702864 0.2589339151 0.0003642046
> fx.rexp(3,2)
[1] 0.1473447 0.1265364 0.3248351