python 的 random lib 真实周到,啥函数都有了。高端的我们用不上,列几个常用的:
随机 [0.0, 1.0)
>>> random.random()
0.906031934904587
多选一
>>> random.choice(['hello', 'good', 'wawa'])
'hello'
随机整数 [a, b]
>>> random.randint(1,10)
8
浮点数随机 [a, b) or [a, b]
>>> random.uniform(1,3.5)
1.3921060878073936
从中选取2个不重复的元素
>>> random.sample([1,2,3,4,5,6], 2)
[6, 3]
乱顺
>>> a = [1,2,3,4,5,6]
>>> random.shuffle(a)
>>> a
[3, 5, 2, 6, 1, 4]
评论