python三十一:random模块

import random

ret = random.random() # (0,1)之间的float数
ret = random.randint(1,10) # [1,10]
ret = random.randrange(1,10)  # [1,10)
ret = random.choice([1, "ab", [4,5,6]]) #  随机出现可迭代对象中的元素
ret = random.sample([1, "ab", [4,5,6], 7, 8, 9], 2) # 控制取出随机数的个数
ret = random.uniform(1,4) # 取指定范围之间的浮点随机数
print(ret)



# 实现验证码功能
def validate_code():
    s = ""
    for i in range(5):
        num = random.randint(0,9)
        alf1 = chr(random.randint(65,90))
        alf2 = chr(random.randint(97,122))
        n = str(random.choice([num,alf1,alf2]))
        s += n
    return s


s = validate_code()
print(s)

猜你喜欢

转载自blog.csdn.net/m0_37564426/article/details/82085559