random模块
Posted 月为暮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了random模块相关的知识,希望对你有一定的参考价值。
import random # 0-1 之间的小数float类型 print(random.random()) #[1,3]的整数 print(random.randint(1,3)) # [1,3)的整数 print(random.randrange(1,3)) # 随机选择一个元素 # choices会生成一个列表 print(random.choices((1,2,23,4))) # choice的返回值是那个元素 print(random.choice((1,2,3,4,5,6))) # 注意:这两个方法一定是在一个可迭代对象中选择 # 随机选择两个组合 print(random.sample([1,‘23‘,[4,6]],2)) # 大于1,小于3的小数 print(random.uniform(1,3)) list1 = [1,2,3,4,5] # 打乱list1的顺序 random.shuffle(list1) print(list1)
以上是关于random模块的主要内容,如果未能解决你的问题,请参考以下文章