random模块

一 random模块运用
import random

# print(random.random())
# print(random.randint(1,3))
# print(random.randrange(1,3))
# print(random.choice([1,'egon',[1,2]]))
# print(random.sample([1, 'aa', [4, 5]], 2))

# print(random.uniform(1,3))

# item=['a','b','c','d']
# random.shuffle(item)
# print(item)


# import time
# def make_progress(precent,width=50):
# if precent>1:precent=1
# a=("[%%-%ds]"%width)%(int(precent*width)*"#")
# print("\r%s %s%%"%(a,int(precent*100)),end="")
#
# total_size=66666
# size=0
# while size<total_size:
# time.sleep(0.1)
# size+=1024
# precent=size/total_size
# make_progress(precent)

import random
def made_code(size=7):
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(made_code())

二 random汇总
random 0-1 开闭
randint 0 - 3 开开
randrange 0 - 3 开闭
choice [1,2,32,3,2,"哈哈"]   
随机选一个 sample ([1,"a",["c","d"]],2)   
随机选指定个数 uniform (1,3)  闭闭 浮点
shuffle (列表) 打乱顺序


posted @ 2018-09-22 01:13  不沉之月  阅读(106)  评论(0编辑  收藏  举报