python学习笔记5--random

一、random模块

import random,string

print(random.randint(1,199))#1-199随机取一个整数
print(string.digits) #所有的数字0-9
print(string.ascii_lowercase) #所有的小写字母
print(string.ascii_uppercase) #所有的大写字母
print(string.ascii_letters) #所有的小写字母+所有的大写字母
print(string.punctuation)  #所有的特殊字符

s = random.choice(['ybq','mpp','zhx','df'])#随机取一个元素
s = random.choice()#随机取一个元素
res = random.sample(string.digits,3) #随机取N个元素
print(''.join(res))

res = random.uniform(1,9)#取随机小数??
print(res)
print(round(res,2))# 保留几位小数,如果四舍五入之后,最后一位小数是0,那么不显示
print(random.random()) #取0-1之间随机小数

s = ['a','b','c','d','e']
random.shuffle(s) #洗牌,打乱顺序,只能传list
print(s)                

猜你喜欢

转载自www.cnblogs.com/SuKiWX/p/8946077.html