numpy.random.seed()

seed()函数是用来控制生成有规律随机数的。

当()中有数值时,生成与该值相关的随机数

import numpy as np


例如, 当()中的值为0时,

np.random.seed(0)
print(np.random.randn(1, 3))
np.random.seed(0)
print(np.random.randn(1, 3))
[[1.76405235 0.40015721 0.97873798]]
[[1.76405235 0.40015721 0.97873798]]


当()中的值为1时,

np.random.seed(1)
print(np.random.randn(1, 3))
np.random.seed(1)
print(np.random.randn(1, 3))
[[ 1.62434536 -0.61175641 -0.52817175]]
[[ 1.62434536 -0.61175641 -0.52817175]]


()中的值固定时,对应生成的随机数是固定不变的,例如,当()中的数为0时,生成的1*3矩阵只有一种,即

[[1.76405235 0.40015721 0.97873798]]


值得注意的是,seed()的效果只有一次,即只对seed()之后离其最近的代码 np.random.randn(1, 3) 起作用,例如

np.random.seed(0)
print(np.random.randn(1, 3))
print(np.random.randn(1, 3))
[[1.76405235 0.40015721 0.97873798]]
[[ 2.2408932   1.86755799 -0.97727788]]


可以看出,np.random.seed(0)对第二段代码 np.random.randn(1, 3) 没作用

  2. 当()中没有数值时,生成与该值无关的随机数

np.random.seed()
print(np.random.randn(1, 3))
print(np.random.randn(1, 3))
[[ 0.42128832 -0.95154595 -1.11228585]]
[[-0.16132993 -0.59064208 -0.32277392]]


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值