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

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.

1 Like

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

3 Likes

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.

13 Likes

Relevant stack overflow:

Cheers,

Colin

3 Likes

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.

1 Like

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.

5 Likes

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.

4 Likes

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.

1 Like

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.

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

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

I think that relying on a specific set of random numbers and a set of corresponding results is a very poor way to test, for the following reasons:

  1. it relies on some other calculation (in some other language, etc) being “correct”, which may or may not hold
  2. it precludes testing on the whole domain (eg with random numbers), it is quite easy to miss eg branches this way
  3. it is essentially a black box, hard to modify and extend
  4. and finally, it can break very easily with otherwise innocuous changes (eg floating point ops being reordered)

I am under the impression that the main motivation for this kind of testing is convenience.

1 Like