This question is twofold. I am translating an R script into C++ that uses the L\'Ecuyer combined multiple recursive generator (CMRG) as it\'s engine (in particular, MRG32k3a), w
I have found that PRNG's with the same seeds across different languages do not necessarily produce the same result (since they may have parameters that the compiler is free to specify) as seen in the SO posts here and here. That is to say, using the same seed, the same engine, and the same distribution may result in different random numbers depending on the particular implementation of the PRNG.
The first answer explains merely that there is no random number sequence that corresponds universally to a given PRNG seed; it may be documented and implemented differently in different APIs (not just in the compiler and not just at a language level). The second answer is specific to rand
and srand
in the C language and is the case because rand and srand use an unspecified algorithm.
Although neither answer touches on random number distributions, they too are important if reproducible "randomness" is desired. In that sense, although C++ guarantees the behavior of the engines it provides, it makes the behavior of its distributions (including uniform_real_distribution) implementation-specific.
In general, problems involving seeding PRNGs for repeatable "randomness" could have been avoided if RNG APIs used a stable (unchanging) and documented algorithm not only for the seeded PRNG, but for any random number methods that use that PRNG (which, in the case of R, include runif
and rnorm
) — in the latter case because the reproducibility of "random" sequences depends on how those methods (not just the PRNG itself) are documented.
Depending on whether you wrote the R code in question, an option may be to write the C++ and R code to use a custom PRNG (as you seem to have done yourself in part) and to use custom implemented algorithms for each random number method the original R code uses (such as runif
and rnorm
). This option may be viable especially since statistical tests are generally insensitive to details of the specific PRNG in use.
Depending on how the R script is written, another option may be to pregenerate the random numbers needed by the code.