首页 > 编程语言 > 详细

python-----内置模块之random模块

时间:2020-03-04 17:52:28      阅读:76      评论:0      收藏:0      [点我收藏+]

random.random()---随机[0,1)的浮点数

random.randint(1,3)---随机1到3的整数[1,3]

random.randrange(1,3)----1,2

random.choice(‘hello‘)      random.choice([1,2,3])----序列中随机取

random.sample(‘hello‘,2)-------序列中随机取两个

random.uniform(1,3)-----取浮点数

a=[1,2,3,4,5,6]

random.shuffle(a)-----洗牌功能

验证码的应用

这是数字的验证码的应用:

import random
checkcode=‘‘
for i in range(4):
    current=random.randint(1,9)
    checkcode+=str(current)
print(checkcode)

这是数字加字母的验证码的应用:

 1 import random
 2 checkcode=‘‘
 3 for i in range(4):
 4     current=random.randrange(0,4)
 5     #随机字母
 6     if current==i:
 7         tmp=chr(random.randint(65,90))
 8     #随机数字
 9     else:
10         tmp=random.randint(0,9)
11 
12     checkcode+=str(tmp)
13 print(checkcode)

需要5位时只需要改动4这个值

for i in range(4) 
current=random.randrange(0,4)

  

 

python-----内置模块之random模块

原文:https://www.cnblogs.com/cy2268540857/p/12410834.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!