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!
twister
instead of the default generator and use python's builtinrandom.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.