Discussion:
[Rcpp-devel] Setting the R random seed from Rcpp
Matteo Fasiolo
2013-06-07 14:22:15 UTC
Permalink
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
Simon Zehnder
2013-06-07 14:35:43 UTC
Permalink
Hi Matteo,

I do not know what you really want to do, but if you want to replicate results, you could do the following in Rcpp before running the RNG:

Rcpp::Environment baseEnv("package:base");
Rcpp::Function setSeed = baseEnv["set.seed"];
setSeed(0); //any other number would do it here


Best
Simon
Post by Matteo Fasiolo
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
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
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Romain Francois
2013-06-07 14:42:24 UTC
Permalink
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
Post by Matteo Fasiolo
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
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
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Matteo Fasiolo
2013-06-07 15:29:24 UTC
Permalink
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
Post by Romain Francois
Environment g = Environment::global_env() ;
Environment::Binding RandomSeed = g[".Random.seed"] ;
NumericVector someVariable = RandomSeed ;
RandomSeed = someVariable ;
The Environment::Binding class acts a a proxy class.
Romain
Post by Matteo Fasiolo
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
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
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Matteo Fasiolo
2013-06-07 15:51:22 UTC
Permalink
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
Post by Matteo Fasiolo
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).
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
Post by Romain Francois
Environment g = Environment::global_env() ;
Environment::Binding RandomSeed = g[".Random.seed"] ;
NumericVector someVariable = RandomSeed ;
RandomSeed = someVariable ;
The Environment::Binding class acts a a proxy class.
Romain
Post by Matteo Fasiolo
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
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
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Dirk Eddelbuettel
2013-06-07 16:12:18 UTC
Permalink
Matteo,

Maybe you may need to figure this out in plain C(++) code based on the
Writing R Extensions manual first. R makes an assumption about keeping the
RNGs in a good state, I am not entirely sure if you actually can do what you
want to do. Just because you can call an R function from Rcpp does not mean
it will do the right thing.

Maybe what you want is more easily done with the RNGs from C++ (esp C++11),
Boost, ... or something different from R. It may work, but we are
(currently) simply not set up for it.

The model is to call set.seed(somenumber) from R before going to C++, and
being assured that a) you get the same stream in C++ as you would in R as
well as b) that you can continue fine in R once you are back.

What you want is a little different. It may well be supported, but maybe
just not (yet ?) by us. Or it may not be supported. Please keep working at
it, this would be useful to sort this out.

Just because you can get the integer seed via R from C++ does yet mean that
the generator is a sane state. Just saying...

Dirk
--
Dirk Eddelbuettel | ***@debian.org | http://dirk.eddelbuettel.com
Krzysztof Sakrejda
2013-06-07 16:22:09 UTC
Permalink
Post by Dirk Eddelbuettel
Maybe what you want is more easily done with the RNGs from C++ (esp C++11),
Boost, ... or something different from R. It may work, but we are
(currently) simply not set up for it.
If you go with something different from R, I've had good luck with Tina's
RNG. (TRNG) It's a fairly complete, fast, and easy to use C++ library...
there might even be a reason to wrap it for Rcpp someday.

Krzysztof
Post by Dirk Eddelbuettel
The model is to call set.seed(somenumber) from R before going to C++, and
being assured that a) you get the same stream in C++ as you would in R as
well as b) that you can continue fine in R once you are back.
What you want is a little different. It may well be supported, but maybe
just not (yet ?) by us. Or it may not be supported. Please keep working at
it, this would be useful to sort this out.
Just because you can get the integer seed via R from C++ does yet mean that
the generator is a sane state. Just saying...
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Matteo Fasiolo
2013-06-07 16:24:32 UTC
Permalink
Dirk,

you are right, intuitively I see that the way I'm doing things doesn't
seem very save. The motivation behind my experimentation is that
I'm trying to move things from R to C++ in order to avoid that segfault
problem that we observed a couple of weeks ago.
Probably I'm clinging a bit too much to R RNGs, but maybe I should
switch to a C++ RNG. The problem is complicated also by the fact
that I'm using the parallel package, so I have to careful about what I do!
Post by Simon Zehnder
Matteo,
Maybe you may need to figure this out in plain C(++) code based on the
Writing R Extensions manual first. R makes an assumption about keeping the
RNGs in a good state, I am not entirely sure if you actually can do what you
want to do. Just because you can call an R function from Rcpp does not mean
it will do the right thing.
Maybe what you want is more easily done with the RNGs from C++ (esp C++11),
Boost, ... or something different from R. It may work, but we are
(currently) simply not set up for it.
The model is to call set.seed(somenumber) from R before going to C++, and
being assured that a) you get the same stream in C++ as you would in R as
well as b) that you can continue fine in R once you are back.
What you want is a little different. It may well be supported, but maybe
just not (yet ?) by us. Or it may not be supported. Please keep working at
it, this would be useful to sort this out.
Just because you can get the integer seed via R from C++ does yet mean that
the generator is a sane state. Just saying...
Dirk
--
William Dunlap
2013-06-07 16:09:10 UTC
Permalink
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-----
Sent: Friday, June 07, 2013 7:42 AM
To: Matteo Fasiolo
Subject: Re: [Rcpp-devel] Setting the R random seed from Rcpp
Environment g = Environment::global_env() ;
Environment::Binding RandomSeed = g[".Random.seed"] ;
NumericVector someVariable = RandomSeed ;
RandomSeed = someVariable ;
The Environment::Binding class acts a a proxy class.
Romain
Post by Matteo Fasiolo
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
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
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-pro
Loading...