[Rcpp-devel] Setting the R random seed from Rcpp
matteo.fasiolo at gmail.com
Fri Jun 7 17:51:22 CEST 2013
Sorry, what I want to do is to call an R function from Rcpp:
List myFun(int n, Function foo)
Environment g = Environment::global_env();
Environment::Binding RandomSeed = g[".Random.seed"];
IntegerVector someVariable = RandomSeed;
output1 = foo();
RandomSeed = someVariable;
output2 = foo();
return List::create(output1, output2);
myRnorm <- function()
This works, the only correction is that .Random.Seed is an IntegerVector.
Thanks a lot!
On Fri, Jun 7, 2013 at 4:29 PM, Matteo Fasiolo <matteo.fasiolo at gmail.com>wrote:
> Thanks for you replies.
> I need to seed the seed because I'm estimating the likelihood
> function in an MCMC algorithm, and I need to use the same random numbers
> in order
> to get a good acceptance ratio. (I could also pass vectors of random
> numbers around but
> that's quite messy).
> For some reason what Romain suggested doesn't seem to work:
> List myFun(int n)
> RNGScope scope;
> Environment g = Environment::global_env();
> Environment::Binding RandomSeed = g[".Random.seed"];
> NumericVector someVariable = RandomSeed;
> NumericVector output1(rnorm(n));
> RandomSeed = someVariable;
> NumericVector output2(rnorm(n));
> return List::create(output1, output2);
>  1.3963522 0.3949708 -0.4526889 0.6898165 0.4216432 0.5742222 -0.6332163 0.2886058 1.9990184 -1.6650286
>  0.33822725 0.19465447 0.04197205 0.01744784 0.75131016 0.06764945 -0.82518198 0.54712702 -2.48995321
>  -0.38688042
> On Fri, Jun 7, 2013 at 3:42 PM, Romain Francois <romain at r-enthusiasts.com>wrote:
>> You can do something like this (i'm on my phone, so you might have to
>> change it):
>> Environment g = Environment::global_env() ;
>> Environment::Binding RandomSeed = g[".Random.seed"] ;
>> You get the current value of the binding like this:
>> NumericVector someVariable = RandomSeed ;
>> And then when you want to set the variable: you just do :
>> RandomSeed = someVariable ;
>> The Environment::Binding class acts a a proxy class.
>> Le 7 juin 2013 à 16:22, Matteo Fasiolo <matteo.fasiolo at gmail.com> a
>> écrit :
>> > Dear Rcpp experts,
>> > I would like to be able to store the R random seed from a Rcpp
>> > function and then reset it. In other words I would like to replicate
>> > the following code in Rcpp:
>> > savedSeed <- .Random.seed
>> > x <- Rfunction(parameters1);
>> > .Random.seed <- savedSeed
>> > x1 <- Rfunction(parameters2);
>> > where Rfunction simulates some random numbers and uses them
>> > but with different parameter values.
>> > Is it possible to do something similar in Rcpp?
>> > Probably using GetRNGstate() and PutRNGstate()?
>> > Thanks,
>> > Matteo
>> > _______________________________________________
>> > Rcpp-devel mailing list
>> > Rcpp-devel at lists.r-forge.r-project.org
>> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Rcpp-devel