- import 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())
- '''
随机数 1: 0.7643602170615428
随机数 2: 0.31630323818329664
随机数 3: 0.13436424411240122
随机数 4: 0.13436424411240122
随机数 5: 0.9560342718892494
可以看到当 seed() 没有参数时, 每次生成的随机数是不一样的, 而当 seed() 有参数时, 每次生成的随机数是一样的, 同时选择不同的参数生成的随机数也不一样
来源: http://www.bubuko.com/infodetail-3210566.html