<div dir="ltr">Sorry, what I want to do is to call an R function from Rcpp:<div><br></div><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> output1 = foo();</div><div><br></div><div> RandomSeed = someVariable;</div><div><br></div><div> NumericVector output2(n);</div><div> output2 = foo();</div><div> </div><div> return List::create(output1, output2);</div>
<div>}</div><div> </div><div>')</div><div><br></div><div>myRnorm <- function()</div><div>{</div><div> rnorm(10)</div><div>}</div><div><br></div><div>myFun(10, myRnorm)</div></div><div><br></div><div style>This works, the only correction is that .Random.Seed is an IntegerVector.</div>
<div style>Thanks a lot!</div><div style><br></div><div style>Matteo</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 7, 2013 at 4:29 PM, Matteo Fasiolo <span dir="ltr"><<a href="mailto:matteo.fasiolo@gmail.com" target="_blank">matteo.fasiolo@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks for you replies.<div><br></div><div>I need to seed the seed because I'm estimating the likelihood <div>
function in an MCMC algorithm, and I need to use the same random numbers in order</div><div>to get a good acceptance ratio. (I could also pass vectors of random numbers around but</div>
<div>that's quite messy).</div><div><br></div><div>For some reason what Romain suggested doesn't seem to work:<div><br></div><div><div>cppFunction('</div><div> </div><div>List myFun(int n)</div><div>{</div><div>
RNGScope scope;</div><div class="im"><div><br></div><div> Environment g = Environment::global_env();</div><div> Environment::Binding RandomSeed = g[".Random.seed"];</div></div><div> NumericVector someVariable = RandomSeed;</div>
<div><br></div><div> NumericVector output1(rnorm(n));</div><div><br></div><div> RandomSeed = someVariable;</div><div><br></div><div> NumericVector output2(rnorm(n));</div><div> </div><div> return List::create(output1, output2);</div>
<div>}</div><div> </div><div>')</div><div><br></div><div><br></div><div>myFun(10)</div></div><div><br></div><div><span style="border-spacing:0px;line-height:16px;border-collapse:separate;font-size:14px;background-color:rgb(225,226,229);white-space:pre-wrap;font-family:'Ubuntu Mono'"><pre 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] 1.3963522 0.3949708 -0.4526889 0.6898165 0.4216432 0.5742222 -0.6332163 0.2886058 1.9990184 -1.6650286
[[2]]
[1] 0.33822725 0.19465447 0.04197205 0.01744784 0.75131016 0.06764945 -0.82518198 0.54712702 -2.48995321
[10] -0.38688042</pre></span></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 7, 2013 at 3:42 PM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
You can do something like this (i'm on my phone, so you might have to change it):<br>
<br>
Environment g = Environment::global_env() ;<br>
Environment::Binding RandomSeed = g[".Random.seed"] ;<br>
<br>
You get the current value of the binding like this:<br>
<br>
NumericVector someVariable = RandomSeed ;<br>
<br>
And then when you want to set the variable: you just do :<br>
<br>
RandomSeed = someVariable ;<br>
<br>
The Environment::Binding class acts a a proxy class.<br>
<br>
Romain<br>
<br>
<br>
Le 7 juin 2013 à 16:22, Matteo Fasiolo <<a href="mailto:matteo.fasiolo@gmail.com" target="_blank">matteo.fasiolo@gmail.com</a>> a écrit :<br>
<div><div><br>
> Dear Rcpp experts,<br>
><br>
> I would like to be able to store the R random seed from a Rcpp<br>
> function and then reset it. In other words I would like to replicate<br>
> the following code in Rcpp:<br>
><br>
> savedSeed <- .Random.seed<br>
> x <- Rfunction(parameters1);<br>
><br>
> .Random.seed <- savedSeed<br>
> x1 <- Rfunction(parameters2);<br>
><br>
> where Rfunction simulates some random numbers and uses them<br>
> but with different parameter values.<br>
> Is it possible to do something similar in Rcpp?<br>
> Probably using GetRNGstate() and PutRNGstate()?<br>
><br>
> Thanks,<br>
><br>
> Matteo<br>
</div></div><div><div>> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>