# How to match random number generator to R?

Although I know the algorithm that R is using for pseudo-random numbers I am unable to match the generation of random numbers.

Needs["RLink"]
InstallR["RHomeLocation" -> "C:\\Program Files\\R\\R-3.4.3"]
REvaluate["RNGkind()"]

{"Mersenne-Twister", "Inversion"}


From the R documentation for Random Number Generation this means that the Mersenne-Twister algorithm is used for pseudo-random numbers and the inversion method is used to create random normals.

Taking a sample of from the standard normal in R.

REvaluate["set.seed(115)"]
rRNorm = REvaluate["rnorm(5)"]

{0.632375, 0.572379, 0.540476, -0.184169, 0.11106}


I expect that I should be able to match this pseudo-random set in Wolfram Language by using the same algorithm and method.

SeedRandom[115, Method -> "MersenneTwister"]
InverseCDF[NormalDistribution[], RandomVariate[UniformDistribution[], 5]]

{-0.77278, 1.25667, -0.584935, 1.39649, 0.191121}


However the set does not match.

I have also tried.

SeedRandom[115, Method -> "MersenneTwister"]
RandomVariate[NormalDistribution[], 5]

{1.37915, -1.06197, 1.39337, -0.780701, 0.809144}
`

With equally unsatisfactory comparison.

How do I match the pseudo-random number generation between R and Wolfram Language? Of course, without having to call R.

• While I may be wrong, I thought that pseudorandom number generators will just create random bits. There are multiple ways to make a random integer or a random floating point number from those. There are lots of steps to get from those random bits to normally distributed real numbers. I think there are also multiple variations of the Mersenne twister (or multiple parameters that can be used). – Szabolcs Oct 25 '18 at 12:09
• It might not be of much help, but may be worth a look anyway mathematica.stackexchange.com/q/19541/12 – Szabolcs Oct 25 '18 at 12:10
• "How to match random number generator to R?" -- Do not. (That was easy.) – Anton Antonov Oct 25 '18 at 14:24
• Very likely the seeds get used to initialize in different ways even though the RNGs are the same. – Daniel Lichtblau Oct 25 '18 at 14:29
• @Edmund What's wrong with calling R? (seriously.) – user202729 Oct 25 '18 at 15:15