From wdunlap at tibco.com  Fri Jun  7 18:09:10 2013
From: wdunlap at tibco.com (William Dunlap)
Date: Fri, 7 Jun 2013 16:09:10 +0000
Subject: [Rd] [Rcpp-devel] Setting the R random seed from Rcpp
In-Reply-To: <45439FB4-7E16-44C5-B87F-275BBB542959@r-enthusiasts.com>
References: <CAC_0WE-jR+DGPRTzOGyWvF=1NnX9giGxcvSVWRv3LVj2E9sW6w@mail.gmail.com>
	<45439FB4-7E16-44C5-B87F-275BBB542959@r-enthusiasts.com>
Message-ID: <E66794E69CFDE04D9A70842786030B931C2FFCAC@PA-MBX01.na.tibco.com>

This would be easier if base::set.seed() accepted a value of .Random.seed
instead of just a scalar integer or, new to R-3.0.0, NULL.  If set.seed() returned the
previous value of .Random.seed (NULL if there was no previous value) things
might be even easier.  People should not have to know where .Random.seed
is stored.

S+'s set.seed() accepts a value of .Random.seed but does not return the previous value.
(If it is given an illegal value for .Random.seed it complains and sets it to a random value.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-
> bounces at lists.r-forge.r-project.org] On Behalf Of Romain Francois
> Sent: Friday, June 07, 2013 7:42 AM
> To: Matteo Fasiolo
> Cc: rcpp-devel at lists.r-forge.r-project.org
> Subject: Re: [Rcpp-devel] Setting the R random seed from Rcpp
> 
> 
> 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.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
> _______________________________________________
> 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


[Rd] [Rcpp-devel] Setting the R random seed from Rcpp
William Dunlap