<div dir="ltr"><div style>Hi all,</div><div style><br></div><div style> I continued experimenting with setting the R seed from an </div><div style>Rcpp function. While this code works:</div><div><br></div><div>cppFunction('</div>
<div> </div><div>List myFun(int n, Function foo)</div><div>{</div><div> RNGScope scope;</div><div><br></div><div> Environment g = Environment::global_env();</div><div> Environment::Binding RandomSeed = g[".Random.seed"];</div>
<div> IntegerVector someVariable = RandomSeed;</div><div><br></div><div> NumericVector output1(n);</div><div> NumericVector output2(n);</div><div> </div><div> output1 = foo(n);</div><div><br></div><div> RandomSeed = someVariable;</div>
<div> output2 = foo(n);</div><div><br></div><div> return List::create(output1, output2);</div><div>}</div><div> </div><div>')</div><div><br></div><div><div>myRnorm <- function(n)</div><div>{</div><div> rnorm(n)</div>
<div>}</div><div><br></div><div>set.seed(524514)</div><div>myFun(10, myRnorm)</div></div><div><br></div><div><span class="" style="border-collapse:separate;color:rgb(0,0,0);font-family:'Ubuntu Mono';font-size:14px;line-height:16px;white-space:pre-wrap;border-spacing:0px;background-color:rgb(225,226,229)"><pre tabindex="0" class="" style="font-family:'Ubuntu Mono';outline:none;border:none;margin-top:0px;margin-bottom:0px;line-height:1.2;font-size:10.4pt!important;white-space:pre-wrap!important">
[[1]]
[1] -0.46949481 -0.40449904 1.10377225 -0.94267114 0.71828154 0.44722386 -0.04653023 0.49021681 -0.68894641
[10] 0.15505136
[[2]]
[1] -0.46949481 -0.40449904 1.10377225 -0.94267114 0.71828154 0.44722386 -0.04653023 0.49021681 -0.68894641
[10] 0.15505136</pre><pre tabindex="0" class="" style="font-family:'Ubuntu Mono';outline:none;border:none;margin-top:0px;margin-bottom:0px;line-height:1.2;font-size:10.4pt!important;white-space:pre-wrap!important">
<br></pre><pre tabindex="0" class="" style="font-family:'Ubuntu Mono';outline:none;border:none;margin-top:0px;margin-bottom:0px;line-height:1.2;font-size:10.4pt!important;white-space:pre-wrap!important">If I put another layer of C++:</pre>
</span></div><div><br></div><div>cppFunction('</div><div>NumericVector nested(int n)</div><div>{</div><div> return rnorm(n);</div><div>}</div><div>')</div><div><br></div><div>myRnorm <- function(n)</div><div>{</div>
<div> nested(n)</div><div>}</div><div><br></div><div>set.seed(524514)</div><div>myFun(10, myRnorm)</div><div><br></div><div><span class="" style="border-collapse:separate;border-spacing:0px;background-color:rgb(225,226,229)"><pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px">
<font color="#000000" face="Ubuntu Mono"><span style="font-size:14px;line-height:16px;white-space:pre-wrap">[[1]]
[1] -0.46949481 -0.40449904 1.10377225 -0.94267114 0.71828154 0.44722386 -0.04653023 0.49021681 -0.68894641
[10] 0.15505136
[[2]]
[1] -1.58817318 0.32828207 0.01351185 -0.28672471 0.51732449 -0.10360312 0.97944543 -0.16814832 -0.83571146
[10] -0.43258866
This doesn't work any more. Luckily I listened to Dirk's warning about messing with R RNG,</span></font></pre><pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><font color="#000000" face="Ubuntu Mono"><span style="font-size:14px;line-height:16px;white-space:pre-wrap">because this strange "sandwich" (C++ calls R calls C++) is pretty much what I was going to </span></font></pre>
<pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><font color="#000000" face="Ubuntu Mono"><span style="font-size:14px;line-height:16px;white-space:pre-wrap">do in my application.</span></font></pre>
<pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><font color="#000000" face="Ubuntu Mono"><span style="font-size:14px;line-height:16px;white-space:pre-wrap"><br></span></font></pre>
<pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><font color="#000000" face="Ubuntu Mono"><span style="font-size:14px;line-height:16px;white-space:pre-wrap">Matteo</span></font></pre>
<pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><span style="font-size:14px;line-height:16px;white-space:pre-wrap;color:rgb(0,0,0);font-family:'Ubuntu Mono'"><br></span></pre>
<pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><span style="font-size:14px;line-height:16px;white-space:pre-wrap;color:rgb(0,0,0);font-family:'Ubuntu Mono'"> </span></pre>
<pre tabindex="0" class="" style="outline:none;border:none;margin-top:0px;margin-bottom:0px"><font color="#000000" face="Ubuntu Mono"><span style="font-size:14px;line-height:16px;white-space:pre-wrap"><br></span></font></pre>
</span></div></div>