I guess they use different methods to generate random numbers, though I thought both are using Mersenne-Twister.
How can I configure Julia or R to use the same method than the other one?
Any alternative or easy method?
I need it to further compare more complex tasks.

I have also tried
rng = MersenneTwister(123)
but the output itâ€™s still different.

Perhaps it would be easier to just generate all your data once and store the result in a file (e.g. a CSV, .mat, or HDF5 file) and then load it from both languages. That will ensure you always have the same data, even if one language or the other changes its random number generation someday.

Maybe you could call function from system libraries? (or some C/C++ libraries?)

For example rand, srand from libc.so?

julia> let seed::UInt32 = 123;ccall((:srand, "libc.so.6"), Cvoid, (Ptr{Cuint},), Ref(seed)) end
julia> ccall((:rand, "libc.so.6"), Cint, (), )
102046953

About rand and srand you could read here where you could find also this:

POSIX.1-2001 gives the following example of an implementation of
rand() and srand(), possibly useful when one needs the same sequence
on two different machines.

static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned int seed) {
next = seed;
}

If you donâ€™t expect cryptographic security I think you could simply implement this in Julia and R.

I think your best solution is the answer from @rdeits, but perhaps you should step back and re-evaluate the problem if you want to use it for testing and rely on any kind of floating point. The reason for this is that there can be minor changes in floating point results across different computers, systems, language versions, and setup, and results can easily diverge, especially if the calculations are not continuous (eg a simple rand(rng) < a can do this).

Testing algorithms that involve randomness is very difficult. I usually rely on testing the deterministic pieces separately, then use test statistics with very low p-value thresholds.

Thank you all. Iâ€™ll have to make do with it.
There is another version of the problem, not about generating real numbers but about sampling from a set of elements.

I had a similar problem: I had some MATLAB code using random numbers that I wanted to reproduce exactly in Julia. I solved it by simply storing the random numbers in MATLAB to a CSV file, and loaded it in Julia.

That turned out to be useful at a later point, when we upgraded from Julia 0.6 to 0.7, since the random number generator changed between the versions, which threw off some of our benchmarks.

I once asked the same question on Slack. The general consensus is that this is pretty difficult unless (as suggested), you call the R library directly or some external library from both R and Julia.

I think this is a good question because as long as we use random number, and if we have to strictly compare code written in different language, we will face the same problem.

Perhaps the easiest way and a robust way, like other guys suggested, you may generate a file which contains enough random numbers, then write function in Julia and R to read those random numbers.

Another way could be to write/get a very small random number generator subroutine by yourself or borrow from other peoplesâ€™, then write it in both Julia and R, so they should generator exactly the same results.
Eg. Malvin Kalos, and Paula Whitlockâ€™s Monte Carlo book, https://onlinelibrary.wiley.com/doi/book/10.1002/9783527626212
There is a chapter called Pseudorandom Numbers in which you could find some small simple algorithms to generate random numbers.
Also, in the book Numerical Recipe and its cd-rom (I am sure you can find all its content on the internet), there should be such small subroutine too.

But I think the purpose of using exact the same random numbers in different language like Julia and R, is to do the testing. Once you confirmed your code is correct, there is no need to use exactly the same random numbers for different languages.