random.seed总结

综述

(1)seed()方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数。(也就是说seed()方法配合随机函数一起使用,当然随机函数可以不要seed(),这样每次生成的随机数都不一样)
(2)当seed()没有参数时,每次生成的随机数是不一样的
(3)当seed()有参数时,每次生成的随机数是一样的,同时选择不同的参数生成的随机数也不一样(比如参数都为1时生成的随机数一样,参数都为2时生成的随机数一样,而1跟2时生成的这两个随机数不一样)

举例

import random

#随机函数前不要 seed()方法
print('随机数0:',random.random())
print('随机数000:',random.random())

# 随机数不一样
random.seed()
print('随机数1:',random.random())
random.seed()
print('随机数2:',random.random())

# 随机数一样
random.seed(1)
print('随机数3:',random.random())
random.seed(1)
print('随机数4:',random.random())
random.seed(2)
print('随机数5:',random.random())
random.seed(2)
print('随机数6:',random.random())

#''''''''''''
#运行结果
随机数00.05028033957842759
随机数0000.8052102548983406
随机数10.8634052345861548
随机数20.09865087436993825
随机数30.13436424411240122
随机数40.13436424411240122
随机数50.9560342718892494
随机数60.9560342718892494

你可能感兴趣的:(random.seed总结)