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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值