Comparing Matlab and Numpy code that uses random number generation

前端 未结 4 541
后悔当初
后悔当初 2020-12-10 06:25

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:

4条回答
  •  旧时难觅i
    2020-12-10 07:00

    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
    

提交回复
热议问题