ndom.uniform(10,20) #生成10,20之间的浮点数
19.774883012515218
>>> random.uniform(10,20)
11.654111952867027
>>> random.randint(10,20) #生成指定范围内的整数
18
>>> random.randint(10,20)
11
>>> random.randrange(1,100,8) #从指定范围内按指定基数递增获取随机数
33
>>> random.randrange(1,100,8)
17
>>> random.randrange(1,100,8)
33
>>> random.choice([1,2,3,4,5]) #从序列元素中随机获取元素,只能是有序类型
2
>>> random.choice([1,2,3,4,5])
1
>>> random.choice([1,2,3,4,5])
2
>>> random.choice('abcd')
'd'
>>> random.choice('abcd')
'a'
>>> random.choice('abcd')
'c'
>>> a = [1,2,3,4,5] #将一个列表元素打乱
>>> random.shuffle(a)
>>> a
[1, 2, 3, 5, 4]
>>> random.shuffle(a)
>>> a
[2, 5, 4, 3, 1]
>>> random.sample(a,2) #从指定序列中随机获取N个元素,生成新对象
[5, 2]
>>> random.sample(a,2)
[5, 4]
>>> random.sample(a,3)
[3, 4, 1]
>>> random.sample(a,5)
[5, 1, 4, 2, 3]
2017-09-2412:27:23?
|