9

Is there some way to make the random number generator in numpy generate the same random numbers as in Matlab, given the same seed?

I tried the following in Matlab:

>> rng(1);
>> randn(2, 2)

ans =

    0.9794   -0.5484
   -0.2656   -0.0963

And the following in iPython with Numpy:

In [21]: import numpy as np
In [22]: np.random.seed(1)
In [23]: np.random.randn(2, 2)
Out[23]: 
array([[ 1.624, -0.612],
       [-0.528, -1.073]])

Values in both the arrays are different.

Or could someone suggest a good idea to compare two implementations of the same algorithm in Matlab and Python that uses random number generation.

Thanks!

1
  • 2
    You could try to use MATLAB's twister instead of the default generator and use python's builtin random.random(). However I doubt that you'll be able to reproduce exactly the same results. I'd say that you shouldn't rely on the random numbers being the same. For a good randomized algorithm the only thing that should matter is whether these numbers are random enough, and I assume both MATLAB and numpy implementations are good enough. If you simply want to create random inputs for testing then simply save them to files and load the files in both MATLAB and python.
    – Bakuriu
    Commented Aug 28, 2013 at 11:39

4 Answers 4

21

Just wanted to further clarify on using the twister/seeding method: MATLAB and numpy generate the same sequence using this seeding but will fill them out in matrices differently.

MATLAB fills out a matrix down columns, while python goes down rows. So in order to get the same matrices in both, you have to transpose:

MATLAB:

rand('twister', 1337);
A = rand(3,5)
A = 
 Columns 1 through 2
   0.262024675015582   0.459316887214567
   0.158683972154466   0.321000540520167
   0.278126519494360   0.518392820597537
  Columns 3 through 4
   0.261942925565145   0.115274226683149
   0.976085284877434   0.386275068634359
   0.732814552690482   0.628501179539712
  Column 5
   0.125057926335599
   0.983548605143641
   0.443224868645128

python:

import numpy as np
np.random.seed(1337)
A = np.random.random((5,3))
A.T
array([[ 0.26202468,  0.45931689,  0.26194293,  0.11527423,  0.12505793],
       [ 0.15868397,  0.32100054,  0.97608528,  0.38627507,  0.98354861],
       [ 0.27812652,  0.51839282,  0.73281455,  0.62850118,  0.44322487]])
1
  • This was really helpful!
    – wwl
    Commented Oct 2, 2017 at 21:24
4

As Bakuriu suggest it works using MATLABs twister:

MATLAB:

>> rand('twister', 1337)
>> rand()

ans =

    0.2620

Python (Numpy):

>>> import numpy as np
>>> np.random.seed(1337)
>>> np.random.random()
0.2620246750155817
1
  • 5
    This works for random.random() but not if you want to compare the MATLAB randperm(10) with numpy.random.permutation(10). They give different results...
    – linello
    Commented Mar 12, 2014 at 9:02
1
  1. One way to ensure the same numbers are fed to your process is to generate them in one of the two languges, save them and import into the other language. This is fairly easy, you could write them in a simple textfile.

  2. If this is not possible or desirable, you can also make sure the numbers are the same by doing the generation of the pseudo random numbers yourself. Here is a site that shows a very simple example of an easy to implement algorithm: Build your own simple random numbers

  3. If the quality of your homemade random generator is not sufficient, you can build a random generation function in one language, and call it from the other. The easiest path is probably to call matlab from python.

  4. If you are feeling lucky, try playing around with the settings. For example try using the (outdated) seed input to matlabs random functions. Or try using different kinds of generators. I believe the default in both languages is mersenne twister, but if this implementation is not the same, perhaps a simpler one is.

0

How about running a matlab script to get the random numbers based upon a seed, from within your python code?

Not the answer you're looking for? Browse other questions tagged or ask your own question.