numpy API: np.random.seed() 与 np.random.RandomState()

np.random.seed()设置种子之后,用np.random.RandomState()记录种子,这样后续生成的随机数都是可以重用的。

例子:

np.random.seed(1234)
np.random.uniform(0, 10, 5)

array([1.9151945 , 6.22108771, 4.37727739, 7.85358584, 7.79975808])
np.random.rand(2,3)

array([[0.27259261, 0.27646426, 0.80187218],
       [0.95813935, 0.87593263, 0.35781727]])
#保存种子后生成的随机数和上次一致
r = np.random.RandomState(1234)
r.uniform(0, 10, 5)

array([1.9151945 , 6.22108771, 4.37727739, 7.85358584, 7.79975808])
r.rand(2,3)

array([[0.27259261, 0.27646426, 0.80187218],
       [0.95813935, 0.87593263, 0.35781727]])

参考:
https://stackoverflow.com/questions/22994423/difference-between-np-random-seed-and-np-random-randomstate

你可能感兴趣的:(numpy)