首页 > 编程语言 > 详细

python random模块

时间:2019-05-15 21:35:32      阅读:138      评论:0      收藏:0      [点我收藏+]
import random

#产生(0,1)的随机数,--float
print(random.random())

#产生[1,3]的整形随机数
print(random.randint(1,3))

#产生[1,3)范围内的整形随机数
print(random.randrange(1,3))

#产生[1,3)范围内的随机数
print(random.uniform(1,3))

#在可迭代对象中产生随机数
print(random.choice([11,22,33]))

#在可迭代对象中产生规定个数的随机数
print(random.sample([11,22,33,44,55],2))

#打乱列表
item=[11,22,33,44,55]
random.shuffle(item)
print(item)

 

 

随机生成验证码示例:

#生成验证码
def v_code():
    ret=‘‘
    for i in range(0,5):
        num=random.randint(0,9)
        alf=chr(random.randint(65,122))#将数字转换为ASCII表中的符号
        s=str(random.choice([num,alf]))
        ret+=s
    return ret

print(v_code())

 

1

python random模块

原文:https://www.cnblogs.com/Manuel/p/10872312.html

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