软件测试|Python random模块,超乎想象的强大

发布时间 2023-09-07 15:08:24作者: 霍格沃兹测试开发学社

在这里插入图片描述

Python的random模块是一个非常强大的工具,用于生成随机数和随机选择。它提供了许多函数和方法,可以满足各种随机化需求。本文将介绍random模块的基本功能和常见用法,以帮助读者更好地理解和利用这个模块。

返回整数

  1. random.randange()

语法如下:

random.randrange(stop)
random.randrange(start, stop[, step])

参数解析:

  • stop: 末尾数字,不包含(取不到 stop 这个值)
  • start: 起始数字,包含(取得到 start 这个值)
  • step: 步长

示例如下:

import random
for i in range(5):
    print(random.randrange(30))
############
9
25
21
3
20
------------------
import random
for i in range(5):
    print(random.randrange(10, 20))
###########
14
12
11
14
13
-------------------

import random

for i in range(5):
    print(random.randrange(20, 30, 2))
############
24
28
22
22
24
  1. random.randint()

语法如下:

random.randint(a, b)

语法说明:

  • 该语句相当于random.randint(a, b+1)
  • 返回随机整数 N 满足 a <= N <= b

示例如下:

import random

for i in range(5):
    print(random.randint(10,20))
######
12
15
10
13
13

注:该方法可以返回两端的值

返回浮点数

  1. random.random()

语法格式:

random.random()

上述语法将返回一个[0.0,1.0)之间的一个浮点数

使用示例

import random

for i in range(5):
    print(random.random())
-----------------
0.051698653828633145
0.10619823413246887
0.33732593583951986
0.6763699334756532
0.16816052346812582


import math
import random

for i in range(6):
    print(math.ceil(random.random() * 1000))

---------------------
67
38
612
639
454
870
  1. random.uniform()

语法格式如下:

random.uniform(a, b)

语法说明:

  • 返回一个随机浮点数 N
  • a <= b 时,a <= N <= b
  • b < a 时, b <= N <= a

使用示例

import random

for i in range(5):
    print(random.uniform(1, 10))

###############
5.457242422186069
8.633135221688587
2.9232627928020625
7.921168676991703
2.07340428271263

---------------------
import random

for i in range(5):
    print(random.uniform(12, 3))

####################
3.1848989873887614
5.15029109210898
3.947059333936842
9.637384540893176
4.816027706079555

以列表作为参数

  1. random.choice()

语法如下:

random.choice(seq)

语法说明:

  • 从非空序列 seq 返回一个随机元素
  • 如果 seq 为空,会抛出 IndexError

示例如下:

import string

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))

#############
3
b
c
d
e
7
H
  1. random.choices()

语法如下:

random.choices(population, weights=None, *, cum_weights=None, k=1) 

参数说明:

  • populaiton:序列
  • weights:普通权重
  • cum_weights:累加权重
  • k:选择次数

注:weights 和 cum_weights 不能同时传,只能选择一个来传

示例如下:

import random
a = [1,2,3,4,5]
print(random.choices(a,k=5))

#########
[4, 3, 5, 3, 5]
---------
a = [1, 2, 3, 4, 5]
print(random.choices(a, weights=[0, 0, 0, 0, 1], k=5))

# 序列有多长,weights 对应的序列就得多长,每个位置都是一一对应
# 像这里,5 的权重是 1,其他是 0 ,所以每次都取 5,因为它的权重最高,其他元素没有权重
########
[5, 5, 5, 5, 5]

------------------------
a = [1, 2, 3, 4, 5]
print(random.choices(a, weights=[0, 0, 2, 0, 1], k=5))

# 2 的权重更大,所以取到它的概率更高
################
[3, 3, 5, 3, 5]
  1. random.shuffle()

语法如下:

random.shuffle(x[, random])
  • 将序列 x 随机打乱位置
  • 只能是列表[],元组、字符串会报错

示例如下:

import random

# 数字数组
a = [1, 2, 3, 4, 5]
random.shuffle(a)
print(a)

# 字母数组
b = ["a", "b", "c"]
random.shuffle(b)
print(b)

######
[4, 3, 2, 1, 5]
['b', 'a', 'c']
  1. random.sample()

语法如下:

random.sample(population, k)

示例如下:

import random
import string

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))

############
[4, 3, 5]
['b', 'c', 'a']
['a', 'b', 'c']
['c', 'a', 'f']
['l', 't', 'y']
['0', '8', '3']
['5', '8', 'T']

总结

Python的random模块提供了许多强大的功能,用于生成随机数和随机选择。无论是用于游戏开发、数据分析还是密码生成等领域,都可以发挥重要作用。通过熟练掌握random模块的使用方法,可以增加程序的随机性和灵活性,为开发者带来更多可能性。希望本文对大家理解和使用random模块有所帮助!

获取更多技术资料,请点击!