random 模块

>>> import random
>>> random.random()
0.42936489785928167
>>> random.randint(1,3)    #包含3
1
>>> random.randrange(1,3)  # 不包含3
1
>>> random.choice([2,3,4,5])
3
>>> random.sample([2,3,4,5,6,7],3)
[5, 3, 2]


>>> string.digits
'0123456789'
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.hexdigits
'0123456789abcdefABCDEF'

>>> string.ascii_lowercase + string.digits
'abcdefghijklmnopqrstuvwxyz0123456789'
>>> 
>>> s=string.ascii_lowercase + string.digits
>>> random.sample(s,5)
['5', 'w', 'v', 'q', 'i']
>>> 

洗牌

>>> l=[1,2,3,4]
>>> random.shuffle(l)
>>> l
[1, 2, 4, 3]
>>> 
原文地址:https://www.cnblogs.com/infaaf/p/8849084.html