random 模块,也就是随机数模块,用来生成一定范围内的随机数。随机数模块可以这样使用:
1 2 3 4 5 6 7 8 9 10 11
| import random print(random.random()) print(random.randint(1, 5)) print(random.randrange(0, 10, 2))
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(random.choice(lst)) print(random.choices(lst, k=5)) print(random.sample(lst, k=5)) random.shuffle(lst) print(lst)
|
输出的结果为:
1 2 3 4 5 6 7
| 0.30540264424342467 2 6 4 [3, 1, 9, 3, 8] [8, 5, 9, 6, 3] [3, 5, 1, 8, 4, 6, 9, 7, 2]
|