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


#1

If I run this code on R

set.seed(123)
runif(1)

I get

0.2875775

But if I run on Julia

using Random
Random.seed!(123);
rand(1,1)

I get

0.7684476751965699

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.


#2

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


#3

I would prefer a solution independent from R


#4

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.


#5

Relevant stack overflow:

Cheers,

Colin


#6

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.


#7

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.


#8

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.


#9

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.


#10

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.