目录
1. seed()介绍
2.seed()参数不改变?
3.seed()参数改变
4.random.seed()放在循环内和循环外的效果
1. seed()介绍
seed(int x): 参数x可有可无,返回的是None。seed()不能单独使用,必须是random.seed()。改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数,一般种子数x不一样,生成的伪随机数也不一样。
import random
print('\n')
print(random.seed(1))
None
错误代码:
print('\n')
print(seed(1))
31 print('\n')
---> 32 print(seed(1))
NameError: name 'seed' is not defined
2.seed()参数不改变?
我们调用 random.random() 生成随机数时,每一次生成的数都是随机的。但是,当我们预先使用 random.seed(x) 设定好种子之后,这个时候,先调用它的情况下,使用 random() 生成的随机数将会是同一个。
如果seed()里面的参数不改变,先调用seed(),再调用random.random()生成伪随机数:
import random
print("------- 设置种子 seed -------")
random.seed( 10 )
for i in range(0,10):
print ("Random number with seed 10 : ", random.random())
print("\n")
# 生成同一个随机数
random.seed( 10 )
for i in range(0,20):
print ("Random number with seed 10 : ", random.random())
print("\n")
------- 设置种子 seed -------
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
Random number with seed 10 : 0.20609823213950174
Random number with seed 10 : 0.81332125135732
Random number with seed 10 : 0.8235888725334455
Random number with seed 10 : 0.6534725339011758
Random number with seed 10 : 0.16022955651881965
Random number with seed 10 : 0.5206693596399246
Random number with seed 10 : 0.32777281162209315
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
Random number with seed 10 : 0.20609823213950174
Random number with seed 10 : 0.81332125135732
Random number with seed 10 : 0.8235888725334455
Random number with seed 10 : 0.6534725339011758
Random number with seed 10 : 0.16022955651881965
Random number with seed 10 : 0.5206693596399246
Random number with seed 10 : 0.32777281162209315
Random number with seed 10 : 0.24999667668640035
Random number with seed 10 : 0.9528169091459117
Random number with seed 10 : 0.9965569925394552
Random number with seed 10 : 0.04455638245043303
Random number with seed 10 : 0.860161037286291
Random number with seed 10 : 0.603190610968185
Random number with seed 10 : 0.3816059859191179
Random number with seed 10 : 0.28361821790671515
Random number with seed 10 : 0.674964847134956
Random number with seed 10 : 0.45683115105830563
这里的种子数是一样的,只是循环次数不一样而已,通过输出,我们可以看到前面的10个数据是一模一样的,但是第二段的后10个数据有所不同,需要说明:
(1) seed(int x),这里的x并不是生成伪随机数的个数
(2)random.random()会生成(0,1)之间的小数,如果想要生成某区间内的整数,利用random.randint(x,y),其中x,y分别是闭区间的下界和上界,比如生成[1,9]内的整数:
random.randint(1, 9)
生成随机数、随机字符串更多请参考: Python random() 函数 | 菜鸟教程 (runoob.com)https://www.runoob.com/python/func-number-random.html
[python] random 模块 -- 生成随机数/打乱序列 - 简书 (jianshu.com)https://www.jianshu.com/p/155bd3e0d78b
3.seed()参数改变
下面看一下改变种子数得到的实验效果:两个循环部分得到了不同的伪随机数
import random
print("------- 设置种子 seed -------")
random.seed( 10 )
for i in range(0,10):
print ("Random number with seed 10 : ", random.random())
print("\n")
# 生成不同随机数
random.seed( 20 )
for i in range(0,10):
print ("Random number with seed 10 : ", random.random())
print("\n")
------- 设置种子 seed -------
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
Random number with seed 10 : 0.20609823213950174
Random number with seed 10 : 0.81332125135732
Random number with seed 10 : 0.8235888725334455
Random number with seed 10 : 0.6534725339011758
Random number with seed 10 : 0.16022955651881965
Random number with seed 10 : 0.5206693596399246
Random number with seed 10 : 0.32777281162209315
Random number with seed 10 : 0.9056396761745207
Random number with seed 10 : 0.6862541570267026
Random number with seed 10 : 0.7665092563626442
Random number with seed 10 : 0.9046162378132736
Random number with seed 10 : 0.2598274474889769
Random number with seed 10 : 0.6357258696059892
Random number with seed 10 : 0.9049456946664788
Random number with seed 10 : 0.8721303740697106
Random number with seed 10 : 0.5729406692492218
Random number with seed 10 : 0.1693780871255699
4.random.seed()放在循环内和循环外的效果
seed()放在循环内且只调用一次random.random():每次生成的都是相同的伪随机数,相当于指针每次都指向同一个地方,输出的数据没有发生改变,数据输出如下:
import random
print("seed()放在循环外数据效果: ")
random.seed( 10 )
for i in range(0,10):
print ("Random number with seed 10 : ", random.random())
print("\n")
print("seed()放在循环内数据效果: ")
for i in range(0,10):
random.seed(10)
print ("Random number with seed 10 : ", random.random())
seed()放在循环外数据效果:
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
Random number with seed 10 : 0.20609823213950174
Random number with seed 10 : 0.81332125135732
Random number with seed 10 : 0.8235888725334455
Random number with seed 10 : 0.6534725339011758
Random number with seed 10 : 0.16022955651881965
Random number with seed 10 : 0.5206693596399246
Random number with seed 10 : 0.32777281162209315
seed()放在循环内数据效果:
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135
seed()放在循环内且调用多次random.random():每个循环段内输出相同,但是循环内单个random.random()此时是不同的
我个人理解:循环内调用的random.random()的次数,相当于从头指针(假设有一个头指针)往后走了几步,访问了几个数据,调用次数越多,那么访问数据也就越多。
import random
print("seed()放在循环内数据效果: ")
for i in range(0,3):
random.seed(10)
print ("Random number with seed 10 : ", random.random())
print ("Random number with seed 10 : ", random.random())
print ("Random number with seed 10 : ", random.random())
print('\n')
seed()放在循环内数据效果:
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.4288890546751146
Random number with seed 10 : 0.5780913011344704
|