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

02-random

时间:2020-03-31 01:21:54      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:imp   随机   for   pytho   range   str   shuff   +=   lan   

一、random模块

random模块是随机取值

import random
print(random.random())# (0,1)--float 大于0且小于1之间的小数 
print(random.randint(1,3))  # [1,3] 大于等于1且小于等于3之间的整数
print(random.randrange(1,3)) # [1,3) 大于等于1且小于3之间的整数
print(random.choice([1,‘23‘,[4,5]])) # 1或者23或者[4,5]
print(random.sample([1,‘23‘,[4,5]],2)) # 列表元素任意2个组合
print(random.uniform(1,3)) # 大于1小于3的小数,如1.927109612082716

item=[1,3,5,7,9]
random.shuffle(item) #打乱item的顺序,相当于"洗牌"
print(item)

二、应用

# 随机验证码
import random
def make_code(size = 4)
    res = ""
    for i in range(size)
        s1 = chr(random.randint(65,90))
        s2 = str(random.randint(0,9))
        res += random.choice([s1,s2])
   	return res

print(make_code(4))

02-random

标签:imp   随机   for   pytho   range   str   shuff   +=   lan   

原文地址:https://www.cnblogs.com/zhuyouai/p/12602481.html

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