numpy.random.seed()函数的思考

转自:https://blog.csdn.net/weixin_31270811/article/details/80287015

numpy中有可以用来产生随机数的函数,这里主要就其中的seed()函数进行一些简单的介绍。 
贴一个官方的链接:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.seed.html#numpy.random.seed 
seed()函数用来控制输出的数,简单来说是为了保证生成有规律的随机数。 
总的来说,seed()函数中的参数是一个整数,它的功能更像是一个标记!!!在参数相同的该函数被调用后,生成的随机数及顺序都是一样的,根据np.random.randn()函数输入参数的尺寸来按顺序输出这些随机数。 

举例证明: 

eg1:seed()函数中的参数是一个标记

import numpy as np  
np.random.seed(1)  
i=0  
while (i<5):  
    print("输出:"+str(np.random.randn(1)))  
    i+=1  
print("hello1")  
np.random.seed(0)  
i=0  
while (i<5):  
    print("输出:"+str(np.random.randn(1)))  
    i+=1  
print("hello2")  
i=0  
np.random.seed(1)  
while (i<5):  
    print("输出:"+str(np.random.randn(1)))  
    i+=1  
print("hello3")  
np.random.seed(0)  
i=0  
while (i<5):  
    print("输出:"+str(np.random.randn(1)))  
    i+=1  
print("hello4")  

结果: 
输出:[1.62434536] 
输出:[-0.61175641] 
输出:[-0.52817175] 
输出:[-1.07296862] 
输出:[0.86540763] 
hello1 
输出:[1.76405235] 
输出:[0.40015721] 
输出:[0.97873798] 
输出:[2.2408932] 
输出:[1.86755799] 
hello2 
输出:[1.62434536] 
输出:[-0.61175641] 
输出:[-0.52817175] 
输出:[-1.07296862] 
输出:[0.86540763] 
hello3 
输出:[1.76405235] 
输出:[0.40015721] 
输出:[0.97873798] 
输出:[2.2408932] 
输出:[1.86755799] 
hello4

eg2:根据np.random.randn()函数输入参数的尺寸来按顺序输出这些随机数
 

import numpy as np
np.random.seed(1)
i=0
while (i<5):
    print("输出:"+str(np.random.randn(1)))
    i+=1
print("hello1")
np.random.seed(0)
i=0
while (i<5):
    print("输出:"+str(np.random.randn(1)))
    i+=1
print("hello2")
i=0
np.random.seed(1)
while (i<2):
    print("输出:"+str(np.random.randn(1,5)))
    i+=1
print("hello3")
np.random.seed(0)
i=0
while (i<2):
    print("输出:"+str(np.random.randn(1,2)))
    i+=1
print("hello4")

结果: 
输出:[1.62434536] 
输出:[-0.61175641] 
输出:[-0.52817175] 
输出:[-1.07296862] 
输出:[0.86540763] 
hello1 
输出:[1.76405235] 
输出:[0.40015721] 
输出:[0.97873798] 
输出:[2.2408932] 
输出:[1.86755799] 
hello2 
输出:[[ 1.62434536 -0.61175641 -0.52817175 -1.07296862 0.86540763]] 
输出:[[-2.3015387 1.74481176 -0.7612069 0.3190391 -0.24937038]] 
hello3 
输出:[[1.76405235 0.40015721]] 
输出:[[0.97873798 2.2408932 ]] 
hello4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值