0

I have the following MatLab code:

randn('seed', 1);
rand('seed', 1);
A = 0.1*randn(5, 10)

And I am trying to write JAVA code that produces exactly the same result.

Here is my JAVA code:

import java.util.Random;
import java.lang.Math;

public class HelloWorld
{
    static double[][]  random_normal_matrix(Random r, int x, int y)
    {
        double tmp[][] = new double[x][y];

        for(int i = 0; i < x; i++)
            for(int j = 0; j < y; j++)
                tmp[i][j] = 0.1*r.nextGaussian();

        return tmp;
    }

    public static void main(String[] args)
    {
        Random r = new Random();
        r.setSeed(1);
        double tmp[][] = random_normal_matrix(r, 5, 10);

        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < 10; j++)
                System.out.print(tmp[i][j]+" ");

            System.out.println();
        }
    }
}

As you can see if you run the code, here https://octave-online.net/ and here https://www.compilejava.net/ the results are very different. The issue is not just some difference in precision.

Can someone please explain how I can get the same results?

NET_GUY
  • 137
  • 5
  • Are you trying to get two random number generators to give you the same behaviour? –  May 04 '17 at 10:31
  • "Random number generation is the generation of a sequence of numbers or symbols that cannot be reasonably predicted better than by a random chance, usually through a random-number generator (RNG)." – e4c5 May 04 '17 at 10:32
  • 1
    I am using the same seed value. Since the output is deterministic, it should be possible to get the same results with the same seed value. – NET_GUY May 04 '17 at 10:34
  • It seems unlikely the MatLab and Java will use exactly the same method to generate random numbers –  May 04 '17 at 10:35
  • 1
    @patrick-hainge Okay, but the method matlab uses should be published somewhere. And I should be able to implement it in JAVA, correct? – NET_GUY May 04 '17 at 10:36
  • may be this helps .. http://stackoverflow.com/questions/35430471/java-vs-matlab-math-random-and-rand – Rajat Srivastava May 04 '17 at 10:37

2 Answers2

3

From the doc page of rng:

...

rng(seed, generator) and rng('shuffle', generator) additionally specify the type of the random number generator used by rand, randi, and randn. The generator input is one of:

  1. 'twister': Mersenne Twister

  2. 'simdTwister': SIMD-oriented Fast Mersenne Twister

  3. 'combRecursive': Combined Multiple Recursive

  4. 'multFibonacci': Multiplicative Lagged Fibonacci

  5. 'v5uniform': Legacy MATLAB® 5.0 uniform generator

  6. 'v5normal': Legacy MATLAB 5.0 normal generator

  7. 'v4': Legacy MATLAB 4.0 generator

...

rng('default') puts the settings of the random number generator used by rand, randi, and randn to their default values. This way, the same random numbers are produced as if you restarted MATLAB. The default settings are the Mersenne Twister with seed 0.

Thus, in MATLAB you can choose the algorithm that is used in the random number generation. You currently use Mersenne-Twister with seed 1, assuming you use a reasonably recent version of MATLAB.

CAUTION: Octave does not necessarily behave the same as MATLAB.

I do not know the Java side of things, so you should look up what algorithm is used in the package you are using.

Type doc rng at MATLAB command line to see the doc page I quoted from.

souty
  • 607
  • 3
  • 10
0

Trying to get same number from two random number generators defeats its purpose. It is extremely unlikely you'll get same results.

Amila
  • 5,195
  • 1
  • 27
  • 46
  • 1
    But I am using the same seed value. The same RNG with the same seed value should always produce the same results. – NET_GUY May 04 '17 at 10:33