python学习笔记(十二)随机数模块

 1 import  random,string
 2 print(random.randint(1,199))#1-199随机取一个整数,包含199
 3 print(string.digits)#所有的数字0-9
 4 print(string.ascii_lowercase)#所有的小写字母
 5 print(string.ascii_uppercase)#所有的大写字母
 6 print(string.ascii_letters)#所有的大写字母+所有的小写字母
 7 print(string.punctuation)#所有特殊字符
 8 
 9 
10 s=random.choice(['ybq','mpp','zhx','df'])#随机取一个元素
11 print(s)
12 
13 res=random.sample(string.digits,3)#随机取N个元素
14 print(res)#['3', '9', '5']
15 print(''.join(res))#395
16 
17 
18 res=random.uniform(1,9)#取随机小数
19 print(res)
20 print(round(res,5))#保留几位小数
21 
22 print(random.random())#取0-1之间随机小数
23 
24 
25 s=['a','b','c','d','e']
26 random.shuffle(s)#洗牌,打乱顺序,只能传list
27 print(s)
原文地址:https://www.cnblogs.com/wxcx/p/8290580.html