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,4)) #大于1小于4的小数,如1.927109612082716

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

 

def make_code(n):
    res=''
    for i in range(n):
        s1=str(random.randint(0,9)) #随机产生数字
        s2=chr(random.randint(65,90)) #随机产生字符
        res+=random.choice([s1,s2])
    return res

print(make_code(7))

 

posted on 2018-08-03 20:25  蜗牛也是妞  阅读(188)  评论(0编辑  收藏  举报