头文件: import random
1、生成一个随机浮点数,范围是0-1:
print random.random()
2、生成指定范围内的随机浮点数:
print random.uniform(a,b)
3、生成指定范围内的随机整数:
print random.randint(a,b)
4、生成指定范围内的任意数:
print randrange(a,b)
生成指定范围内的任意n个递增序列:
print randrange(a,b,n)
5、随机获取一个元素
random.choice(sequence)
random.choice("Hello world!")
random.choice(("Hello world!"))
random.choice(["Hello world"])
random.choice("Hello","world")
random.choice(("Hello","world"))
random.choice(["Hello","world"])
6、将元素打乱
import random
list=['I','love','Python']
random.shuffle(list)
print list
7、从一个序列中随机选择n个元素,不改变原始序列
import random
a="123456"
b=[1,2,3,4,5,6]
c=['a','b','c','d','e']
print random.sample(a,3)
print random.sample(b,3)
print random.sample(c,3)
另:
a=["123456"]
b="123456"