Hi, I am constructing a function that does sampling in C++ using a non-R RNG stream for thread safety reasons. This C++ function is wrapped by an R function, which is user facing. The R wrapper does some sampling itself to initialize some variables before passing them off to C++. So that my users do not have to manage two mechanisms to set random seeds, I've constructed a solution (shown below) that allows both RNGs to be seeded with set.seed and respond to the state of R's RNG stream. I believe the below works. However, I am hoping to get feedback from more experienced useRs as to whether or not the below approach is unsafe in ways that may affect reproducibility, modify global variables in bad ways, or have other unintended consequences I have not anticipated. Could I trouble one or more folks on this list to weigh in on the safety (or perceived wisdom) of using R's internal RNG stream to seed an RNG external to R? Many thanks in advance. This relates to a Stackoverflow question here: https://stackoverflow.com/questions/63165955/is-there-a-best-practice-for-using-non-r-rngs-in-rcpp-code Pseudocode of a trivial facsimile of my current approach is below. --Tommy sample_wrapper <- function() { # initialize a variable to pass to C++ init_var <- runif(1) # get current state of RNG stream # first entry of .Random.seed is an integer representing the algorithm used # second entry is current position in RNG stream # subsequent entries are pseudorandom numbers seed_pos <- .Random.seed[2] seed <- .Random.seed[seed_pos + 2] out <- sample_cpp(init_var = init_var, seed = seed) # move R's position in the RNG stream forward by 1 with a throw away sample runif(1) # return the output out} [[alternative HTML version deleted]]
Duncan Murdoch
2020-Jul-30 19:36 UTC
[Rd] Seeding non-R RNG with numbers from R's RNG stream
I wouldn't trust the C++ generator to be as good if you seed it this way as if you just seeded it once with your phone number (or any other fixed value) and let it run, because it's probably never been tested to be good when run this way. Is it good enough for the way you plan to use it? Maybe. Duncan Murdoch On 30/07/2020 3:05 p.m., Tommy Jones wrote:> Hi, > > I am constructing a function that does sampling in C++ using a non-R RNG > stream for thread safety reasons. This C++ function is wrapped by an R > function, which is user facing. The R wrapper does some sampling itself to > initialize some variables before passing them off to C++. So that my users > do not have to manage two mechanisms to set random seeds, I've constructed > a solution (shown below) that allows both RNGs to be seeded with set.seed > and respond to the state of R's RNG stream. > > I believe the below works. However, I am hoping to get feedback from more > experienced useRs as to whether or not the below approach is unsafe in ways > that may affect reproducibility, modify global variables in bad ways, or > have other unintended consequences I have not anticipated. > > Could I trouble one or more folks on this list to weigh in on the safety > (or perceived wisdom) of using R's internal RNG stream to seed an RNG > external to R? Many thanks in advance. > > This relates to a Stackoverflow question here: > https://stackoverflow.com/questions/63165955/is-there-a-best-practice-for-using-non-r-rngs-in-rcpp-code > > Pseudocode of a trivial facsimile of my current approach is below. > > --Tommy > > sample_wrapper <- function() { > # initialize a variable to pass to C++ > init_var <- runif(1) > > # get current state of RNG stream > # first entry of .Random.seed is an integer representing the algorithm used > # second entry is current position in RNG stream > # subsequent entries are pseudorandom numbers > seed_pos <- .Random.seed[2] > > seed <- .Random.seed[seed_pos + 2] > > out <- sample_cpp(init_var = init_var, seed = seed) > > # move R's position in the RNG stream forward by 1 with a throw away sample > runif(1) > > # return the output > out} > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Thank you for this. I'd like to be sure I understand the intuition correctly. Is the following true from what you said? I can just fix the seed at the C++ level and the results will still be (pseudo) random because the initialization at the R level is (pseudo) random. On Thu, Jul 30, 2020 at 3:36 PM Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> I wouldn't trust the C++ generator to be as good if you seed it this way > as if you just seeded it once with your phone number (or any other fixed > value) and let it run, because it's probably never been tested to be > good when run this way. Is it good enough for the way you plan to use > it? Maybe. > > Duncan Murdoch > > On 30/07/2020 3:05 p.m., Tommy Jones wrote: > > Hi, > > > > I am constructing a function that does sampling in C++ using a non-R RNG > > stream for thread safety reasons. This C++ function is wrapped by an R > > function, which is user facing. The R wrapper does some sampling itself > to > > initialize some variables before passing them off to C++. So that my > users > > do not have to manage two mechanisms to set random seeds, I've > constructed > > a solution (shown below) that allows both RNGs to be seeded with set.seed > > and respond to the state of R's RNG stream. > > > > I believe the below works. However, I am hoping to get feedback from more > > experienced useRs as to whether or not the below approach is unsafe in > ways > > that may affect reproducibility, modify global variables in bad ways, or > > have other unintended consequences I have not anticipated. > > > > Could I trouble one or more folks on this list to weigh in on the safety > > (or perceived wisdom) of using R's internal RNG stream to seed an RNG > > external to R? Many thanks in advance. > > > > This relates to a Stackoverflow question here: > > > https://stackoverflow.com/questions/63165955/is-there-a-best-practice-for-using-non-r-rngs-in-rcpp-code > > > > Pseudocode of a trivial facsimile of my current approach is below. > > > > --Tommy > > > > sample_wrapper <- function() { > > # initialize a variable to pass to C++ > > init_var <- runif(1) > > > > # get current state of RNG stream > > # first entry of .Random.seed is an integer representing the > algorithm used > > # second entry is current position in RNG stream > > # subsequent entries are pseudorandom numbers > > seed_pos <- .Random.seed[2] > > > > seed <- .Random.seed[seed_pos + 2] > > > > out <- sample_cpp(init_var = init_var, seed = seed) > > > > # move R's position in the RNG stream forward by 1 with a throw away > sample > > runif(1) > > > > # return the output > > out} > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-devel at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > >[[alternative HTML version deleted]]