python随机数种子的特性
发布时间:2024-07-04 14:29:34

说明
1、在多次重复调用中产生的随机数不同。
无论任何类型的随机数函数,一次随机数种子确定后;
2、当同一随机数种子再次声明时,随机数将从“头”开始。
随机数按相同的顺序生成。这里的“头”是random.seed(seed)声明结束后,首次调用随机数函数;
3、产生的随机数将不同于以前的运行结果(随机数种子为0)。
若指定不同的随机数种子(seed=无论任何随机数函数如何,99)。
以上几点解释了随机数种子每次产生相同随机数的具体含义。事实上,这里还有一个更常见的内涵,即环境独立和跨平台。
实例
importrandom
#print(help(random))
deftest_random_seed_in_std_lib(seed=0,cnt=3):
random.seed(seed)
print("testseed:",seed)
for_inrange(cnt):
print(random.random())
print(random.randint(0,100))
print(random.uniform(1,10))
print('\n')
test_random_seed_in_std_lib()
testseed:0
0.8444218515250481
97
9.01219528753418
0.04048437818077755
65
5.373349269065314
0.9182343317851318
38
9.710199954281542
test_random_seed_in_std_lib()
testseed:0
0.8444218515250481
97
9.01219528753418
0.04048437818077755
65
5.373349269065314
0.9182343317851318
38
9.710199954281542
test_random_seed_in_std_lib(99)
testseed:99
0.40397807494366633
25
6.39495190686897
0.23026272839629136
17
7.8388969285727015
0.2511510083752201
49
5.777313434770537以上是python随机数种子的特点,希望对大家有所帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。
