random模块

import random
#得到一个随机数
print(random.random()) #0.5419014838427241
print(random.randint(1,3)) #2
print(random.randrange(1,5)) #4
print(random.choice([11,22,33])) #33
print(random.sample([11,22,33,44,55],2)) #[22, 44]
print(random.uniform(1,4)) #1.7954211482169886
#打乱列表
ret = [1,2,3,4,5,6]
random.shuffle(ret)
print(ret) #[3, 5, 2, 4, 1, 6]

#验证码函数
def code():
    v_code=""
    for i in range(5):
        num = random.randint(0,9)
        alf1 = chr(random.randint(65,90))
        alf2 = chr(random.randint(97, 122))
        a = random.choice([num,alf1,alf2])
        v_code += str(a)
    return v_code
print(code())
原文地址:https://www.cnblogs.com/zhangsenzhen/p/9401145.html