打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python语言

random模块

random 方法返回随机生成的一个实数,它在[0,1)范围内。

>>> help(random.random)

Help on built-in function random:

random(...) method of random.Random instance

random -> x in the interval [0, 1).

1、random.random

>>> import random

>>> random.random

2、random.randint(1,3),随机整数

3、random.randrange(1,3),随机生成0-2的数字,不包括3

4、random.choice,随机字符

5、random.sample,多个字符中选取特定数量的字符

6、random.uniform,随机浮点数

7、random.shuffle(传入参数),洗牌功能

随机验证码的获取

代码:

import random

checkcode=''

for i in range(4):

current=random.randrange(0,4)

if current==i:

tmp=chr(random.randint(65,90))

else:

tmp=random.randint(0,9)

checkcode+=str(tmp)

print(checkcode)

结果:

7W27

T286

31M8

[Finished in 0.2s]

结语

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python代码实例
个人整理Python代码实例
网站上的验证码是怎么产生的?
python——字符串及练习
python练习册 0002随机生成验证
Python字典推导式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服