python全栈闯关--19-python常用模块-random

# -*- coding:utf-8 -*-
import random
# 随机小数
print(random.random()) # 大于0且小于1之间的小数
print(random.uniform(1, 3)) # 大于1且小于3的小数

# 随机整数
print(random.randint(1,5)) # 大于等于1且小于等于5的整数
print(random.randrange(1,10,1)) # 大于等于1且小于10之间的奇数

# 随机选择
l = [1,2,3,[2,3],'a','b']
print(random.choice(l)) # 随机返回一个
print(random.sample(l,2)) # 随机返回多个值,第二个参数,未随机返回的值

#打乱顺序
print(l)
random.shuffle(l)
print(l)

# 验证码练习
def c_code(num):
    code = ''
    for i in range(num):
        nu = random.randint(0,9)
        c = chr(random.randint(65,90))
        add = random.choice([nu,c])
        code = ''.join([code,str(add)])
    return code


print(c_code(5))
原文地址:https://www.cnblogs.com/zxw-xxcsl/p/12626422.html