5

I'm comparing the mersenne twister in Java and matlab. I'm using the same seed in both. My problem is that when i print out ten numbers from each number generator (Mersenne Twister running in Java and Matlab respectively), the resulting output doesn't seem to match. The output data from the Matlab version prints out every second number from the program in Java.

Java prints:

0.417, 0.997, 0.720, 0.932, 0.0001..

Matlab prints:

0.417, 0.720, 0.0001..

Can anyone point me in the right direction to figure out why this happens?

Java:

public class TestRand {
    static MersenneTwister r = new MersenneTwister(1);

    public static void main(String[] args) {

        int ant = 10;
        float[] randt = new float[ant];

        for (int i = 0; i < ant; i++){
            randt[i] = r.nextFloat()*1;
            System.out.println(randt[i]);    
        }
        System.out.println("------------twist");
    }
}

Matlab:

s = RandStream('twister','Seed',1)
RandStream.setGlobalStream(s);

r = 1 .* rand(1,10);

I am using the standard implementation of the Mersenne Twister in MatLab, the Java-version I am using can be found here

slayton
  • 20,123
  • 10
  • 60
  • 89
  • where did you get the java implementation? – Denis Tulskiy Feb 14 '13 at 14:29
  • 1
    There are 32-bit and 64-bit variants of the Mersenne Twister, which produce different sequences - perhaps Java uses one and Matlab the other? – DNA Feb 14 '13 at 14:29
  • Sorry, I seemed to overlook that piece of info in the initial post. It has now been updated. The Java-version is from [here](http://www.cs.gmu.edu/~sean/research/mersenne/MersenneTwister.java) – user2072220 Feb 14 '13 at 14:30
  • I'm guessing that has to do with different implementations of the algorithm. The best solution would be to contact the author of Java class (http://www.cs.gmu.edu/~sean/research/) or reach out to the Mathworks as the exact details of how they have implemented the random number generator are probably not available to the public. – slayton Feb 14 '13 at 14:37
  • 1
    Note that the Java code is only producing single-precision values, so it only needs 24 bits of the random stream per value produced. My guess is that each sample in the Java code uses the next 32-bit value generated from the MT, while each (presumably double precision) value in Matlab is constructed out of two successive 32-bit MT values. – Mark Dickinson Feb 15 '13 at 08:46
  • I can confirm the output presented matches what 64 bit MATLAB 2012b generates. Could anyone try the 32 bits version? – Dennis Jaheruddin Feb 15 '13 at 15:28

1 Answers1

2

The Matlab rand() results are 64-bit double values. But you're calling nextFloat() to get 32-bit values from the Java MersenneTwister. Check that Java source - nextDouble uses twice as much of the randomness as nextFloat, with two calls to next(). Replace nextFloat() with nextDouble() in your TestRand Java code and your results should line up better.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85