2
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ 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). $\endgroup$ – Szabolcs Oct 25 '18 at 12:09
  • $\begingroup$ It might not be of much help, but may be worth a look anyway mathematica.stackexchange.com/q/19541/12 $\endgroup$ – Szabolcs Oct 25 '18 at 12:10
  • 1
    $\begingroup$ "How to match random number generator to R?" -- Do not. (That was easy.) $\endgroup$ – Anton Antonov Oct 25 '18 at 14:24
  • 1
    $\begingroup$ Very likely the seeds get used to initialize in different ways even though the RNGs are the same. $\endgroup$ – Daniel Lichtblau Oct 25 '18 at 14:29
  • 2
    $\begingroup$ @Edmund What's wrong with calling R? (seriously.) $\endgroup$ – user202729 Oct 25 '18 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.