Python__random模块

#Author wangmengzhu
import random
print(random.random())##随机生成大于0且小于1之间的小数
print(random.randint(1,3))#大于等于1且小于等于3之间的整数
print(random.randrange(1,3))#[1,3)
print(random.choice([1,'23',[4,5]]))#随机选择一个元素
print(random.sample([1,'23',[4,5]],2))#列表元素任意选择两个组合
print(random.uniform(1,3))#大于1小于3的小数

item = [1,3,5,7,9]
random.shuffle(item)#打乱item的顺序,相当于洗牌
print(item)

#生成随机验证码
def make_code(n):
res = ''
for i in range(n):
s1 = str(random.randint(0,9))
s2 = chr(random.randint(65,90))
res += random.choice([s1,s2])
return res
print(make_code(10))
原文地址:https://www.cnblogs.com/wangmengzhu/p/7306092.html