码迷,mamicode.com
首页 > 其他好文 > 详细

random 模块

时间:2018-09-03 13:41:52      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:一个   之间   个数   随机   大于   验证码   col   turn   生成   

随机生成小数

print(random.random()) #大于0且小于1之间的小数
print(random.uniform(1,3)) #大于1小于3的小数

随机生成整数

print(random.randint(1,5))  #大于等于1且等于5之间的整数
print(random.randrange(1,10,2)) #大于等于1且小于10之间的奇数

随机选择一个返回

print(random.choice([1,"23",[4,5]])) #随机返回1或者23或者[4,5]

随机选择多个返回

print(random.sample([1,"23",[3,5]],2)) # 2位返回的个数

打乱列表顺序

item = [1,3,5,7,8]
random.shuffle(item)
print(item)

生成随机验证码

import random

def v_code():

    code = ‘‘
    for i in range(5):

        num=random.randint(0,9)
        alf=chr(random.randint(65,90))
        add=random.choice([num,alf])
        code="".join([code,str(add)])

    return code

print(v_code())

 

random 模块

标签:一个   之间   个数   随机   大于   验证码   col   turn   生成   

原文地址:https://www.cnblogs.com/wanglan/p/9577596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!