I have searched on stackoverflow for people facing similar issues and this topic Replicating MATLAB's `randperm` in NumPy is the most similar.
然而,尽管可以使用NumPy随机排列在Python 中重新创建MatLab中的随机数函数的行为,但是生成的数字并不相同,即使我为两种语言 Select 了相同的种子生成器.我有点困惑,因为我的测试是与其他随机函数相关的,在MatLab和Python 之间.
以下是我try 过的:
MatLab
rng(42);
randperm(15)
它会返回
ans =
11 7 6 5 15 14 1 4 9 10 3 13 8 2 12
Python
np.random.seed(42)
print(np.random.permutation(range(1,16)))
它会返回
[10 12 1 14 6 9 3 2 15 5 8 11 13 4 7]
How can I change my Python code so it can reproduce the same order of random numbers than MatLab ?