python_82_标准库_random模块

import random
print(help(random.random))
#随机整数
print(random.randint(1,7))#生成一个[1, 7]的随机整数
print(random.randrange(1,3))#生成一个[1, 3)的整数数
print(random.randrange(4,10,2))#结果相当于从[4,6,8]序列中获取一个随机数
print(random.choice(range(4,10,2)))#效果与上式相同
#随机字符:
'random.choice接受字符串,列表,元组序列'
print(random.choice('hello'))
print(random.choice([1,3,4]))
print(random.choice((1,2,3,4)))
#多个字符中选取特定数量的随机字符:
print(random.sample('hello',2))
print(random.sample([1,3,4],2))
print(random.sample((1,2,3,4),3))
#随机浮点数:
print(random.uniform(1,10))#指定区间的随机浮点数
print(random.random())#生成一个[0, 1)的随机浮点数
#洗牌 
items=[1,2,3,4,5,6,7] 
random.shuffle(items) 
print(items) 
print(random.shuffle(items))#打印的是random.shuffle函数的返回值None
 

  

原文地址:https://www.cnblogs.com/tianqizhi/p/8432871.html