[Rd] Seeding non-R RNG with numbers from R's RNG stream

Tommy Jones jone@@tho@@w @end|ng |rom gm@||@com
Thu Jul 30 21:05:11 CEST 2020


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]]



More information about the R-devel mailing list