Rand in Julia vs MATLAB

question

#1

Hi,

Based on this issue, https://github.com/JuliaLang/julia/issues/8606, I see we are generating different random number sequence in Julia and MATLAB. Is their a way to get same random numbers in both languages ? I see a comment ‘you can get that by implementing the same RNG as matlab/python in Julia in a package, but not in Base.’ and I am not sure how exactly to do that

Thanks


Comparing random sequences between Julia, Numpy, MATLAB
#2

You would either have to

  1. implement the RNG of the other languages in Julia,
  2. find a C/C++/Fortran library that does this, and write a thin wrapper in Julia.

Doing the first properly is not a trivial task, but it would teach you a lot about the internals of RNGs. Doing the second may be easier.

However, Matlab documentation suggests that they also have a SIMD version, so you could use that and compare to Julia’s default RNG first.


#3

Why do you want to get the same random numbers in the two languages?


#4

For example, if i want to benchmark some sorting algorithms in different languages, i would start with same initial data-set. Other option is to use a fixed data-set (hard-coded).


#5

“However, Matlab documentation2 suggests that they also have a SIMD version, so you could use that and compare to Julia’s default RNG first”

I verified that and the numbers are different


#6

I would suggest generating the test data in one language or the other and saving it as a .mat file (you can use MAT.jl to read mat files in Julia). That will give you reliably identical data which you can also save for posterity if you want.


#7

Alternatively, if it is feasible to test your sorting algorithm on thousands of randomly generated datasets, the average performance should be robust to the composition of your data.


#8
  1. Find an independent random number generator written in C/C++, write a mex interface for it in Matlab and ccall it from Julia.

#9

Thank you all. I ended up using (rdeits suggestion)
“I would suggest generating the test data in one language or the other and saving it as a .mat file (you can use MAT.jl to read mat files in Julia). That will give you reliably identical data which you can also save for posterity if you want.”