Sorry, what I want to do is to call an R function from Rcpp: cppFunction('
List myFun(int n, Function foo) { RNGScope scope; Environment g = Environment::global_env(); Environment::Binding RandomSeed = g[".Random.seed"]; IntegerVector someVariable = RandomSeed; NumericVector output1(n); output1 = foo(); RandomSeed = someVariable; NumericVector output2(n); output2 = foo(); return List::create(output1, output2); } ') myRnorm <- function() { rnorm(10) } myFun(10, myRnorm) This works, the only correction is that .Random.Seed is an IntegerVector. Thanks a lot! Matteo On Fri, Jun 7, 2013 at 4:29 PM, Matteo Fasiolo <matteo.fasi...@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: > > cppFunction(' > > 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); > } > > ') > > > myFun(10) > > [[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 > > > > On Fri, Jun 7, 2013 at 3:42 PM, Romain Francois > <rom...@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. >> >> Romain >> >> >> Le 7 juin 2013 à 16:22, Matteo Fasiolo <matteo.fasi...@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@lists.r-forge.r-project.org >> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel >> > >
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel