random随机结果获取
生成[0, 1)之间浮点数
import random
print("random():", random.random()) # random(): 0.3033243688032643
随机生成给定区间整数
print(random.randint(10, 20)) # 11
随机生成区间的偶数
print(random.randrange(0, 21, 2)) # 14
随机生成区间的浮点数
print(random.uniform(1, 10)) # 3.0533115507714514
从序列中随机选一个
print(random.choice([1, 2, 3, 4, 5])) # 2
从字符串中随机选一个元素
print(random.choice('abcdefg')) # c
对列表元素重新排序
l = [1, 2, 3, 4, 5]
random.shuffle(l)
print(l) # [2, 1, 4, 3, 5]
从字符串中随机去几个字母
print(random.sample('abcdefg', 2)) # ['a', 'f']
从列表中随机去几个元素
print(random.sample([1, 2, 3, 4, 5], 3)) # [3, 2, 4]