随机生成指定位数的验证码

 
  
import random
import string

# 方法一:
def code_1(m, choice):
code=''.join(random.sample(choice, m))
return code
print(code_1(4, string.ascii_letters + string.digits))
 
  
# 方法二:
def code_2(n):
code=''
for i in range(n):
number=random.randint(0, 9) # 0-9
lower_char=chr(random.randint(97, 122)) # a-z
upper_char=chr(random.randint(65, 90)) # A-Z
char_1=random.choice([number, lower_char, upper_char])
code+=str(char_1)
return code

print(code_2(4)
 

先上代码。。。高效如我 ^ ^..。颜色怪怪的、不要介意哈~

Random详解

随机产生一个1-100之间的整数
print(random.randint(1,100))

随机产生一个0-100之间的奇数
print(random.randrange (0,100,2))

随机产生一个0-100之间的偶数
print(random.randrange ( 1 , 100 , 2))

随机产生一个浮点数
print(random.random())
print(random.uniform(1,10))

随机选择一个字符
print(random.choice(r'qwertyuiop[]\asdfghjkl;zxcvbnm,./'))

 随机生成指定数量的字符
print(code=''.join(random.sample(choice, m)))

 其中choice为备选字符串,m为生成的验证码个数

 

转载于:https://www.cnblogs.com/aqin1012/p/11615371.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值