2

I'm having problems to compare the output of two code because of random number state.

I'm comparing the MATLAB randperm function with the output of the equivalent numpy.random.permutation function but, even if I've set the seed to the same value with a MATLAB rand('twister',0) and a python numpy.random.seed(0) I'm obtaining different permutations.

I've to say that the result of MATLAB's rand and numpy numpy.random.rand are the same if the seed are set like above.

linello
  • 8,451
  • 18
  • 63
  • 109
  • They may be using different random number generation algorithms. In that case, even if the seed is the same, the output will be different. – Ffisegydd Mar 12 '14 at 09:12
  • If you just need to compare to codes, you can output a sufficient number of your random permutations using either Matlab or Python and save them to a file. Then the other version can just read in the values from file. This may be the simplest workaround. – horchler Mar 12 '14 at 17:08

1 Answers1

1

This is a common issue. While the random number generator is identical, the function which converts your random number stream into a random permutation is different. There is no specified standard algorithm which describes the expected result.

To solve this issue, you have to use the same library in both tools.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Some suggestion how to do that? – linello Mar 12 '14 at 10:52
  • 1
    There are many possibilities. I don't know what you are planning to do, thus I can't give a recommendation. Some ideas that come to my mind: 1) Generate a dll from your relevant matlab functions, call it from python 2) Create a standalone exe from the m-functions, call it from python 3) Call the m-functions via com or similar automation 4) Call the python code in matlab using system shell 5) Use `org.python.util.PythonInterpreter` in matlab to have access to python. – Daniel Mar 12 '14 at 10:59
  • 1
    Just implement your own permutation algorithm in exactly the same way in python and matlab, for sure you can find some recipes on the web. – Bas Swinckels Mar 12 '14 at 13:47