Rand in Julia vs MATLAB

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

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.

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

1 Like

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).

“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

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.

5 Likes

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.

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

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.”

I wonder if someone has made any progress on this. I have a similar issue to @rohith14 and found my way using @rdeits suggestion.

I am estimating a simple optimization algorithm using Julia’s optimize(..., NelderMead()) from the package ‘Optim.jl’ and MATLAB’s fminsearch().

The estimation involves simulating random draws. The seed in Julia is through Random.seed!() from Random.jl whereas in MATLAB is set using rand('state',...).

However, the only way to get results in Julia that are close to what I get in MATLAB is by importing the simulated data from MATLAB (through MAT.jl).

Does anyone know if it is possible to get the same random numbers in both languages?

Thanks!

The Xoshiro-family pseudorandom generators are straightforward to implement, if you don’t need maximum performance. This is the reference implementation for xoshiro256++:

uint64_t next(void) {
	const uint64_t result = rotl(s[0] + s[3], 23) + s[0];

	const uint64_t t = s[1] << 17;

	s[2] ^= s[0];
	s[3] ^= s[1];
	s[1] ^= s[2];
	s[0] ^= s[3];

	s[2] ^= t;

	s[3] = rotl(s[3], 45);

	return result;
}

So I guess you just need to implement it in Matlab, given that Julia comes with it in the Random stdlib. Start with the same seed in Julia and Matlab, of course.

2 Likes