python-random模块
一、概述
random是一个随机数模块
二、模块方法
1、random.random()
随机返回一个[0,1)的浮点数
input:
import random
res = random.random()
print(res)
output:
0.04363318409773431
2、random.randint(a,b)
产生一个[a,b]的随机整数
input: res = random.randint(1,10) print(res) output: 10
3、random.randrange(1,3)
产生一个[a,b)范围的随机整数
input: res = random.randrange(1,3) print(res) output: 1
4、random.choice(seq)
在seq序列中随机取一个值
input: res = random.choice([1,2,3,4,5]) print(res) output: 4
5、random.sample(self, population, k)
在population中随机取k个值,返回的是一个列表
input: res = random.sample([1,2,3,4,5],2) print(res) output: [4, 1]
6、random.uniform(a,b)
参数一个[a,b]或[b,a]范围内的浮点数
input: res1 = random.uniform(1,3) res2 = random.uniform(3,1) print(res1) print(res2) output: 2.956400821919304 2.779983288293531
7、random.shuffle(x, random=None)
打乱x序列的顺序
input: lis = [1,2,3,4,5] random.shuffle(lis) print(lis) output: [1, 2, 5, 3, 4]
浙公网安备 33010602011771号