python-random库
·
random模块是Python标准库中用于生成伪随机数的模块。下面介绍常用的一些用法:
一、返回整数
1.1random.randrange
1.1.1语法格式
两种写法
random.randrange(stop)
random.randrange(start, stop[, step])
- start:起始数字,包含(取得到 start 这个值)
- stop:末尾数字,不包含(取不到 stop 这个值)
- step:步长
1.1.2实际例子
# 例子一
for i in range(5):
print(random.randrange(20))
####
17
4
7
7
4
# 例子二
for i in range(5):
print(random.randrange(10, 20))
####
13
14
11
17
17
# 例子三
for i in range(5):
print(random.randrange(10, 20, 2))
####
12
12
14
14
10
1.2random.randint
1.2.1语法格式
random.randint(a, b)
- 返回随机整数 N 满足
a <= N <= b - 相当于
randrange(a, b+1)
1.2.2实际例子
for i in range(5):
print(random.randint(0,20))
####
19
20
11
6
3
二、返回浮点数
2.1random.random()
2.1.1语法格式
random.random()
返回 [0.0, 1.0) 范围内的下一个随机浮点数
2.1.2实际例子
# 例子一
for i in range(5):
print(random.random())
####
0.9829492243165335
0.43473506430105724
0.5198709187243076
0.6437884305820736
0.7216771961168909
# 例子二
for i in range(5):
print(math.ceil(random.random() * 1000))
####
772
352
321
62
127
2.2random.uniform(a, b)
2.2.1语法格式
random.uniform(a, b)
- 返回一个随机浮点数 N
- 当
a <= b时,a <= N <= b - 当
b < a时,b <= N <= a
2.2.2实际例子
# 例子一
for i in range(5):
print(random.uniform(1, 10))
####
2.6200262089754593
9.220506911469235
3.0206896704014783
9.670905330339174
1.170694187192196
# 例子二
for i in range(5):
print(random.uniform(8, 2))
####
2.696842757954265
6.058794935110275
7.567631220015144
2.2057698202258074
4.454083664106361
三、返回从指定列表的随机元素
3.1random.choice
3.1.1语法格式
random.choice(seq)
- 从非空序列 seq 返回一个随机元素
- 如果 seq 为空,会抛出 IndexError
3.1.2实际例子
# 数字数组
print(random.choice([1, 2, 3, 4, 5]))
# 字母数组
print(random.choice(["a", "b", "c"]))
# 字母元组
print(random.choice(("a", "b", "c")))
# 字符串
print(random.choice("abcdef"))
# string 模块返回的大小写字母字符串
print(random.choice(string.ascii_letters))
# string 模块返回的数字字符串
print(random.choice(string.digits))
# string 模块返回的数字字符串+大小写字母字符串
print(random.choice(string.digits + string.ascii_uppercase))
####
5
c
c
e
l
2
F
3.2random.choices
3.2.1语法格式
random.choices(population, weights=None, *, cum_weights=None, k=1)
-
population:必需参数,指定要进行选择的序列(可以是列表、元组、字符串等)。
-
weights:可选参数,指定每个元素的权重(概率)。如果不指定,则默认每个元素的权重相等。
-
cum_weights:可选参数,指定累计权重。如果指定了cum_weights,则必需省略weights参数。
-
k:可选参数,指定要选择的元素个数。默认为1,即只选择一个元素。
从序列中有放回地随机抽取k个元素,并返回对应的数组,weights参数代表每个元素被选到的相对权重,而cum_weights代表的是累积权重,它们的长度需和待抽取序列population长度一致。若无设置权重,则每个元素被抽到的权重是一样的。
注意:权重不能为负值,否则会报错。
3.2.2实际例子
import random
fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon']
weights = [0.1, 0.2, 0.3, 0.2, 0.2]
cum_weights = [0.1, 0.4, 0.7, 0.9, 1.0]
# 从列表中随机选择一个元素
chosen_fruit = random.choices(fruits)
# 选择多个元素
chosen_fruits = random.choices(fruits, k=3)
# 按照概率从列表中随机选择一个元素
chosen_fruit = random.choices(fruits, weights=weights)
# 利用cum_weights参数选择元素
chosen_fruit = random.choices(fruits, cum_weights=cum_weights)
3.3random.shuffle
3.3.1语法格式
random.shuffle(x)
- 将序列 x 随机打乱位置
- 只能是列表[],元组、字符串会报错
3.3.2实际例子
# 数字数组
a = [1, 2, 3, 4, 5]
random.shuffle(a)
print(a)
# 字母数组
b = ["a", "b", "c"]
random.shuffle(b)
print(b)
####
[3, 5, 2, 4, 1]
['a', 'c', 'b']
3.4random.sample
3.4.1语法格式
random.sample(population, k)
- 从 population 中取 k 个元素,组成新的列表并返回
- 每次取元素都是不重复的,所以 population 的长度必须 ≥ k,否则会报错
3.4.2实际例子
# 数字数组
print(random.sample([1, 2, 3, 4, 5], 3))
# 字母数组
print(random.sample(["a", "b", "c"], 3))
# 字母元组
print(random.sample(("a", "b", "c"), 3))
# 字符串
print(random.sample("abcdef", 3))
# string 模块返回的大小写字母字符串
print(random.sample(string.ascii_letters, 3))
# string 模块返回的数字字符串
print(random.sample(string.digits, 3))
# string 模块返回的数字字符串+大小写字母字符串
print(random.sample(string.digits + string.ascii_uppercase, 3))
####
[2, 1, 3]
['b', 'c', 'a']
['a', 'b', 'c']
['a', 'f', 'b']
['M', 'w', 'W']
['7', '1', '5']
['R', '8', 'O']
更多推荐

所有评论(0)