7
$\begingroup$

As a part of my research studies, I have developed a code which is essentially based on random generation of numbers. The first version of the code was written in MATLAB, however, for some reasons I transferred it to Mathematica for further development. In order to assure the correctness of my code in Mathematica, I decided to compare the corresponding results with those from MATLAB. It happened that I was not able to carry out my comparison quantitatively, as I couldn't tweak the random number generators to give the same quantities.

As written in the Mathematica documentation, the possible random number generation methods are as follows:

Enter image description here

On the other hand, the MATLAB documentation reads

Enter image description here

So as it appears, the Mersenne Twister is the only common algorithm between MATLAB and Mathematica, but, as shown below, it does not generate the same number in the two environments.

Enter image description here

Enter image description here

What is the reason for this?

$\endgroup$
7
  • 3
    $\begingroup$ You'll probably have to write your own PRNGs. Implementation details matter, and that's only under you control if you do the coding yourself. Fortunately, PRNG algorithms aren't terribly complicated. If you don't need great quality, something as simple as a linear congruential generator (en.wikipedia.org/wiki/Linear_congruential_generator) may work fine. Lagged Fibonacci generators (en.wikipedia.org/wiki/Lagged_Fibonacci_generator) are better, fast, and also pretty easy to code. $\endgroup$
    – John Doty
    Sep 26 at 13:38
  • 4
    $\begingroup$ Perhaps you could call Matlab through Mathematica directly? matlink.org $\endgroup$
    – Chip Hurst
    Sep 26 at 13:39
  • 1
    $\begingroup$ Related: 184614. $\endgroup$
    – Edmund
    Sep 26 at 14:27
  • 7
    $\begingroup$ Maybe just write the rng-numbers from matlab into a file and load it in Mathematica to sample from it. $\endgroup$ Sep 26 at 15:07
  • $\begingroup$ @JulienKluge Unfortunately I want to run a complementary process in Mathematica to one that is in MATLAB and I need to match random number generation to do so. $\endgroup$
    – KratosMath
    Sep 26 at 16:31
16
$\begingroup$

If it is solely for regression testing, the quality of the random numbers doesn't matter very much. You can substitute a better random number generator once you know the Mathematica version matches the MATLAB version.

In which case, you might use something like

Mathematica

Module[{seed = 123456789},
 random := With[{a = 69069, b = 1},
   seed = Mod[a seed + b, 2^32];
   N[seed/2^32]
   ]
 ]

    Table[random, {5}]
(* {0.355504, 0.286935, 0.320493, 0.135751,0.213987} *)

Octave

function u = Random
  persistent seed
  if isempty(seed)
    seed = uint64(123456789);
  end

  a = uint64(69069);
  b = uint64(1);
  seed = mod(a * seed + b, 2^32);
  u = (2^-32) * double(seed);
end

Octave:> [Random, Random, Random, Random, Random]
ans =

   0.35550   0.28694   0.32049   0.13575   0.21399
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.