Python3 random

>>> import random
>>> random.random()
0.27958153262125485
>>> random.randint(1,10)
6
>>> random.randint(1,10)
4
>>> l = [1,2,3,4,5,6]
>>> random.choice(l) #l中随机取一个
5
>>> random.choice(l)
5
>>> random.choice(l)
3
>>> random.choice(l)
5
>>> random.choice(l) 
2
>>> random.sample(l,2) #从l中随机取两个
[1, 4]
>>> random.sample(l,2)
[1, 3]
>>> random.sample(l,2)
[1, 4]
>>> random.randrange(1,100) #整数
70
>>> random.randrange(1,100)
36

随机验证码:

import random
def authentic():
    code_all = []
    for i in range(48,58):
        code_all.append(chr(i))
    for k in range(65,91):
        code_all.append(chr(k))
    for m in range(97,123):
        code_all.append(chr(m))
    # number = random.randint(48,57) #数字
    # letter_big = random.randint(65,90) #大写
    # letter_little = random.randint(97-122)#小写
    n = 0
    code = []
    while n<5:
        code += random.choices(code_all)  #每次随机选择一个字符
        n += 1
    authentic_code= ''.join(code) #拼接为字符串
    print('验证码为:',authentic_code)
authentic()
#方法2==========================================
def autehntic2():
    authentic_code = ''
    n = 0
    while n<5:
        ord_id = random.randrange(48,122)
        if 48<=ord_id<=57 or 65<=ord_id<=90 or 97<=ord_id<=122:
            authentic_code += str(chr(ord_id))
            n+=1
        else:
            continue
    print("验证码:",authentic_code)
autehntic2()
#方法3==========================
def authentic3():
    code= ''
    for i in range(5):
        add = random.choice([random.randint(0,9),chr(random.randrange(65,90)),chr(random.randrange(97,122))])
        code += str(add)
    print(code)
authentic3()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值