random
模块提供了各种用于伪随机数的函数.
随机整数
1. getrandbits(m)
获取包含m
个随机位的长整数. 这里的位指的的是二进制位
2. randint(a, b)
返回随机整数x
, a <= x <= b
3. randrange(start, stop[, step])
返回随机整数 start <= x < b
实值随机数
random()
返回[0.0, 1.0)
之间的随机浮点数. 这个函数是其他所有函数的根本.
随机序列
1. choice(seq)
从非空的序列seq
中返回一个随机元素.
2. sample(seq, len)
返回长度为len
的列表, 它的元素是从seq
中随机抽取的.
3. shuffle(x)
随机打乱列表x
中的元素
from random import *
x = [10, 3, 2, 8, 4]
shuffle(x, random)
print(x)