解释:random.seed() 可以给随机数设置种子,使用相同的种子会生成相同的随机值
下面使用两个种子,一个0,一个1
相同 体现在随机数与种子的距离,与相同种子距离相同的随机数相同
import random
random.seed(0)
print("Random number 1 : ", random.random())
random.seed(1)
print("Random number 11 : ", random.random())
# Random number 1 : 0.8444218515250481
# Random number 11 : 0.13436424411240122
# 生成同一个随机数
random.seed(0)
print("Random number 2 : ", random.random())
print("Random number 3 : ", random.random())
random.seed(1)
print("Random number 22 : ", random.random())
print("Random number 33 : ", random.random())
# Random number 2 : 0.8444218515250481
# Random number 3 : 0.7579544029403025
# Random number 22 : 0.13436424411240122
# Random number 33 : 0.8474337369372327
# 生成同一个随机数
random.seed(0)
print("Random number 4 : ", random.random())
print("Random number 5 : ", random.random())
print("Random number 6 : ", random.random())
random.seed(1)
print("Random number 44 : ", random.random())
print("Random number 55 : ", random.random())
print("Random number 66 : ", random.random())
# Random number 4 : 0.8444218515250481
# Random number 5 : 0.7579544029403025
# Random number 6 : 0.420571580830845
# Random number 44 : 0.13436424411240122
# Random number 55 : 0.8474337369372327
# Random number 66 : 0.763774618976614
|