20190810—random函数用法汇总

 1 import random
 2     # 利用import导入random函数模块
 3 list1=random.randint(0,99)
 4     # random.randint:随机取0至99之间的整数
 5 print(list1)
 6 
 7 list2=random.randrange(0,100,3)
 8     #random.randrange:随机取0至100之间能整除3的数值
 9 print(list2)
10 
11 list3=random.random()
12     #random.random:随机取0至1之间的浮点数
13 print(list3)
14 
15 list4=random.uniform(2,30)
16     #random.uniform:随机取0至1之间的浮点数
17 print(list4)
18 
19 list5=random.choice('周末去公司加班')
20 print(list5)
21 a=random.choice(['嘉航','逸白','子墨','子初'])
22     #random.choice:随机取字符串中的一个字符;同理,也可以随机选取一个字符串,注意:必须是列表的形式
23 print('请抽取一个适合小九的名字:{}'.format(a))
24 
25 a='逸白子墨沐航'
26 list6=random.sample(a,2)
27     #random.sample:多字符中选取特定数量(2个)的字符
28 print('随机选择两个字符作为小九的名字:{},寓意好'.format(list6))
29 
30 list7=[1,3,4,5,6]
31 random.shuffle(list7)
32     #random.shuffle:将list7中的数字进行洗牌,随机进行调整,注意:list7必须是列表的形式,print打印的时候直接引用列表list7。
33 print(list7)
原文地址:https://www.cnblogs.com/yssshiny/p/11331630.html