###random
import random
#随机小数
#0到1之间的小数
print(random.random())
#大于x小于y的小数
print(random.uniform(4,10))
#随机整数
#大于等于x小于等于y的整数
print(random.randint(0,1))
#大于等于1且小于等于10之间的奇数
print(random.randrange(1,10,2))
#随机选择一个返回
print(random.choice([1,4,'e','he']))
#随机选择多个返回,返回的个数为函数的第二个参数
print(random.sample([1,'23',[5,6],'w'],3))
#随机打乱顺序
l=[1,'23',[5,6],'w']
random.shuffle(l)
print(l)
#练习题:随机生成4位验证码,可以包含数字大小写字母
code2=''
for i in range(4):
a=chr(random.randint(65,90))#随机获取大写字母
b=chr(random.randint(97,122))#随机获取小写字母
c=str(random.randint(0,9))#随机获取整数
code1=random.choice([a,b,c])#随机获取从字母数字里取一个值
code2+=code1#字符串加
print(code2)
转载于:https://www.cnblogs.com/lixiaoxuan/articles/9025366.html