1. random.random()生成一个0到1的浮点数 0<=x<1
random.uniform(a,b) 生成一个浮点数 a<=x<=b
random.randit(a,b) 生成一个整数 a<=x<=b
random.randrange(start,stop,step) 范围从start到stop,以step为步长的所有数字中随机选一个
import random
>>> random.random()
0.19822792982467308
>>> random.uniform(3,8)
7.593783144843997
>>> random.uniform(9,2)
5.263024019211441
>>> random.randint(3,100)
32
>>> random.randrange(1,20,3)
10
2. random.choice(sequence) 从sequence中随机获取一个元素。sequence可以是:list,tuple,字符串
>>> import random
>>> List=[1,2,3,4,5,6,7]
>>> Tuple=(11,22,33,44,55,66,77,88)
>>> String="hello,python"
>>> random.choice(List)
4
>>> random.choice(Tuple)
55
>>> random.choice(String)
'h'
3. random.shuffle( a[ ] ) 打乱列表中的元素,随机排列
>>> import random
>>> List=[1,2,3,4,5,6,7]
>>> random.shuffle(List)
>>> List
[7, 5, 2, 6, 3, 1, 4]
4. random.sample( sequence, len) 从sequence序列中随机获取长度为len的片段并随机排列,返回列表
>>> import random
>>> List=[1,2,3,4,5,6,7]
>>> random.sample(List,5)
[4, 5, 2, 7, 1]
>>> Tuple=(11,22,33,44,55,66,77,88,99,100,200,300,400,500)
>>> random.sample(Tuple,5)
[22, 100, 500, 55, 66]
>>> random.sample("HELLOPYTHON",3)
['N', 'Y', 'O']