random模块

import random
random.random( ) 0到1之间的任意小数
random.randint(1,3) 1到3之间的整数
random.randrange(1,3) 顾头不顾尾,[1,2]
random.choice([1,"a",[1,2,3]])指定范围
random.sample([1,2,3,4,5],3)从指定范围取出一定数量
random.uniform(1,3) (1,3)之间的小数,可以指定范围
item = [1,3,5,7,9]
random.shuffle(item) 洗牌

def make_code(n):
s1 = str(random.randint(0,9))
s2 = chr(random.randint(65,90))
s3 = chr(random.randint(97,122))
res = s1+s2+s3
for i in range(n-3):
s = random.choice([str(random.randint(0,9)),chr(random.randint(65,90)),chr(random.randint(97,122))])
res += s
print(res)
return res
make_code(6)

posted @ 2018-12-04 17:09  robertzhou  阅读(102)  评论(0编辑  收藏  举报