本文主要是介绍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
这篇关于numpy.random.seed()函数的思考的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!