如果要设置调用np.random... 将使用的种子,请使用np.random.seed:
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]])
使用类来避免影响全局 numpy 状态:
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]])
您可以通过以下方式查看“全局”类的状态:
np.random.get_state()
和你自己的类实例:
r.get_state()