random模块的学习

时间:2022-04-27 23:03:57
import random
# ret = random.random() #随机取0-1中间的浮点数
# ret = random.randint(1,3) #随机取1-3中间的整数
# ret = random.randrange(1,3) #取左不取右
# ret = random.choice([11,22,33]) #从列表中随机选择一个元素
# ret = random.sample([11,22,33,44,55],2) #从列表中随机选取2个元素
# ret = random.uniform(1,5) #选取1-5中间的浮点型小数
# ret = [1,2,3,4,5]
# random.shuffle(ret) #打乱列表中元素的顺序
# print(ret) #生成一个四位随机数
def v_code():
ret = ''
for i in range(4):
s = random.randint(0,9)
v = chr(random.randint(65,122))
#print([s,v])
i = str(random.choice([s,v]))
ret += i
return ret ret = v_code()
print(ret)