Python---random模块

random模块的常用函数

import random
print(random.random())  #获得0-1之间的随机浮点数
print(random.randint(1,3))  #生成[1,3]的整数
print(random.randrange(1,3))   #顾头不顾尾
print(random.choice([1,2,3,4,5,6]))  #参数可以是任意序列类型,返回序列中的一个值
print(random.sample([1,2,3,4,5,6],2)) #同上,但随机取两个元素以列表形式返回
print(random.sample("abcdefghijklm",2))
print(random.sample(('z','x','y','m','n'),2))
print(random.uniform(1,3)) #生成1~3的随机浮点数
test=[1,2,3,4,5,6,7]
random.shuffle(test)   #打乱次序  参数不能是元组
print(test)

验证码例子

#4位验证码(包含数字,大小写字母)
checkcode=''
for i in range(4):
    cur=random.randrange(0,4)
    if i==cur:
       cur=random.randint(0,9)
    elif cur==1:
        cur=chr(random.randint(65,90))
    else:
        cur=chr(random.randint(97,122))
    checkcode+=str(cur)
print(checkcode)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值