一、使用RANDOM库
random库是python的内置库。
1、使用random.randrange(list_length)用于返回介于0到list_index-1之间的一个随机值。
import random
random_value = random.randrange(len(list))
2、使用random.choice(list)将列表作为输入,返回列表中的一个随机元素。
import random
random_value = random.choice(list)
3、使用random.sample(list_name, number)返回列表中指定number个随机元素。
import random
random_value = random.sample(list, number)
二、使用secrets库
目录
1、使用random.randrange(list_length)用于返回介于0到list_index-1之间的一个随机值。
2、使用random.choice(list)将列表作为输入,返回列表中的一个随机元素。
3、使用random.sample(list_name, number)返回列表中指定number个随机元素。
1、使用secrets.randbelow(list_length)用于返回介于0到list_index-1之间的一个安全随机索引。