How to get the same "random" numbers on Julia and R. seed


If I run this code on R


I get


But if I run on Julia

using Random

I get


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.


The best way to get an RNG to match exactly would probably be to use RCall.jl.


I would prefer a solution independent from R


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.


Relevant stack overflow:




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

For example rand, srand from

julia> let seed::UInt32 = 123;ccall((:srand, ""), Cvoid, (Ptr{Cuint},), Ref(seed)) end

julia> ccall((:rand, ""), Cint, (), )

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.