python随机数种子的特性

536次阅读
没有评论

python随机数种子的特性

说明

1、在多次重复调用中生成的随机数不同。

随机数函数无论任何分布任何类型,在确定了一次随机数种子后;

2、当再次声明相同的随机数种子时,随机数将从“头”开始。

按相同的顺序生成随机数。这里的“头”,即是random.seed(seed)声明后,随机数函数的首次调用;

3、生成的随机数将不同于,之前的(随机数种子为0)的运行结果。

若指定不同的随机数种子(seed=99),无论任何随机数函数。

上面的几点解释了随机数种子可以使得每次生成相同随机数的具体含义。这里的相同,其实还有一种更普遍的内涵,即环境独立和跨平台。

实例

import random
 
# print(help(random))
 
def test_random_seed_in_std_lib(seed=0, cnt=3):
    random.seed(seed)
    print("test seed: ", seed)
    for _ in range(cnt):
        print(random.random())
        print(random.randint(0,100))
        print(random.uniform(1, 10))
        print('\n')
test_random_seed_in_std_lib()
test seed:  0
0.8444218515250481
97
9.01219528753418
 
0.04048437818077755
65
5.373349269065314
 
0.9182343317851318
38
9.710199954281542
test_random_seed_in_std_lib()
test seed:  0
0.8444218515250481
97
9.01219528753418
 
0.04048437818077755
65
5.373349269065314
 
0.9182343317851318
38
9.710199954281542
test_random_seed_in_std_lib(99)
test seed:  99
0.40397807494366633
25
6.39495190686897
 
0.23026272839629136
17
7.8388969285727015
 
0.2511510083752201
49
5.777313434770537

以上就是python随机数种子的特性,希望对大家有所帮助。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-05-26发表,共计1034字。
新手QQ群:570568346,欢迎进群讨论 Python51学习