Python - random 库的详细使用
前言
- 为啥突然写这个?因为用到就写呗,感觉对生成数据很有用,之前都是百度别人的,今天来对着官方文档写,超级标准!
- 这边只讲常用的,看了下文档还有什么数学方法,太高级好像用不上
返回整数
random.randrange
语法格式
两种写法
random.randrange(stop)
random.randrange(start, stop[, step])
- start:起始数字,包含(取得到 start 这个值)
- stop:末尾数字,不包含(取不到 stop 这个值)
- step:步长
实际栗子
# 栗子一
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
random.randint
语法格式
- 返回随机整数 N 满足
a <= N <= b - 相当于
randrange(a, b+1)
random.randint(a, b)
实际栗子
for i in range(5):
print(random.randint(0,20)) ####
19
20
11
6
3
a、b 都可以取得到哦
返回浮点数
random.random()
语法格式
返回 [0.0, 1.0) 范围内的下一个随机浮点数
random.random()
实际栗子
# 栗子一
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
random.uniform(a, b)
语法格式
- 返回一个随机浮点数 N
- 当
a <= b时,a <= N <= b - 当
b < a时,b <= N <= a
random.uniform(a, b)
实际栗子
# 栗子一
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
传递列表作为参数
random.choice
语法格式
- 从非空序列 seq 返回一个随机元素
- 如果 seq 为空,会抛出 IndexError
random.choice(seq)
实际栗子
# 数字数组
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
random.choices
语法格式
- populaiton:序列
- weights:普通权重
- cum_weights:累加权重
- k:选择次数
- weights 和 cum_weights 不能同时传,只能选择一个来传
random.choices(population, weights=None, *, cum_weights=None, k=1)
看的迷迷糊糊啥意思。。?来看栗子。。
不带参数的栗子
a = [1,2,3,4,5]
print(random.choices(a,k=5)) # 结果
[5, 5, 3, 1, 5]
可以重复取元素
带 weight 的栗子一
a = [1, 2, 3, 4, 5]
print(random.choices(a, weights=[0, 0, 1, 0, 0], k=5)) # 结果
[3,3,3,3,3]
- 序列有多长,weights 对应的序列就得多长,每个位置都是一一对应
- 像这里,3 的权重是 1,其他是 0 ,所以每次都取 3,因为它的权重最高,其他元素没有权重
带 weight 的栗子二
a = [1, 2, 3, 4, 5]
print(random.choices(a, weights=[0, 2, 1, 0, 0], k=5)) # 结果
[2, 2, 2, 2, 3]
2 的权重更大,所以取到它的概率更高
带 cum_weights 的栗子
a = [1, 2, 3, 4, 5] print(random.choices(a, cum_weights=[1, 1, 1, 1, 1], k=5)) print(random.choices(a, cum_weights=[1, 4, 4, 4, 4], k=5)) print(random.choices(a, cum_weights=[1, 2, 3, 4, 5], k=5)) # 结果
[1, 1, 1, 1, 1]
[2, 2, 1, 2, 1]
[5, 5, 1, 4, 2]
是不是看不懂?我也看不懂,但其实就是普通权重相加而已
cum_weights=[1, 1, 1, 1, 1]
- 等价于 weights=[1, 0, 0, 0, 0]
- [1,1+0,1+0+0,1+0+0+0,1+0+0+0+0]
- 看懂了没,太反人类了。。
cum_weights=[1, 4, 4, 4, 4]
- 等价于 weights=[1, 3, 0, 0, 0]
- [1,1+3,1+3+0,1+3+0+0,1+3+0+0+0]
random.shuffle
语法格式
- 将序列 x 随机打乱位置
- 只能是列表[],元组、字符串会报错哦
- random 暂时没找到有什么用,可以忽略
random.shuffle(x[, random])
实际栗子
# 数字数组
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']
random.sample
语法格式
- 从 population 中取 k 个元素,组成新的列表并返回
- 每次取元素都是不重复的,所以 population 的长度必须 ≥ k,否则会报错
random.sample(population, k)
实际栗子
全都是 k=3
# 数字数组
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']
Python - random 库的详细使用的更多相关文章
- python random库
random模块 >>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.766433866365 ...
- Python - random库介绍
- python标准库总的random函数用法
Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等.random中的一些重要函数的用法:1 ).random() 返回0& ...
- Python使用turtle库与random库绘制雪花
记录Python使用turtle库与random库绘制雪花,代码非常容易理解,画着玩玩还是可以的. 完整代码如下: 效果图如下:
- Python小游戏——猜数字教程(random库教程)
今天来开发一个简单的数字逻辑游戏,猜数字(数字炸弹) 首先开发游戏第一件事,了解需求. 猜数字游戏规则: 计算机随机生成一个指定范围的数字,由玩家来猜测, 之后计算机会根据玩家提供数字来与自己生成的数 ...
- python基础 — random库
python中用于生成伪随机数的函数库是random 因为是标准库,使用时候只需要import random random库包含两类函数,常用的共8个 --基本随机函数: seed(), random ...
- python之random库的使用以及程序的异常处理
1.random库的使用: random库是使用随机数的Python标准库从概率论角度来说,随机数是随机产生的数据(比如抛硬币),但时计算机是不可能产生随机值,真正的随机数也是在特定条件下产生的确定值 ...
- python学习笔记(8)--random库的使用
伪随机数:采用梅森旋转算法生成的伪随机序列中元素 使用random库 一.基本随机函数 随机数需要一个种子,依据这个种子通过梅森旋转算法产生固定序列的随机数.seed(a=None) 初始化给定的随 ...
- 史上最详细 Python第三方库添加方法 and 错误解决方法
(1):如何添加python第三方库(方法一): File ->> Settings... ->> Project Interpreter (2):如何添加python第三方库 ...
随机推荐
- 《Effective C++》部分内容学习笔记整理
简介 此笔记为<Effective C++>中部分内容的学习笔记. 目录 文档:<Effective C++>
- 数据结构之Queue | 让我们一块来学习数据结构
前面的两篇文章分别介绍了List和Stack,下面让我们一起来学习Queue 数据结构之List | 让我们一块来学习数据结构 数据结构之Stack | 让我们一块来学习数据结构 队列的概况 队列是一 ...
- 1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
- LinqToObject和LinqToSql的区别
抓住五一假期尾巴和小伙伴们一起分享这两者的区别.大家在日常编码的过程当中肯定也注意过或者使用过.但是二者其实存在本质的区别 1.什么是LinqToObject呢? LINQ to Objects指直接 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(九)——一步一步教你如何撸Dapr之OAuth2授权
Oauth2授权,熟悉微信开发的同学对这个东西应该不陌生吧.当我们的应用系统需要集成第三方授权时一般都会做oauth集成,今天就来看看在Dapr的语境下我们如何仅通过配置无需修改应用程序的方式让第三方 ...
- PE文件附加数据感染之Worm.Win32.Agent.ayd病毒分析
一.基本信息 样本名称:1q8JRgwDeGMofs.exe 病毒名称:Worm.Win32.Agent.ayd 文件大小:165384 字节 文件MD5:7EF5D0028997CB7DD3484A ...
- Linux下Apache服务的部署和配置
目录 Apache服务的安装 yum源安装: 目录文件 源码包安装: 目录文件: Apache中添加对php的支持 Apache中添加php对mysql数据库的支持 Apache服务的高级配置 1:配 ...
- Dockerfile多阶段构建
多阶段构建 之前的做法: 在Docker17.05版本之前,构建Docker镜像,通常采用两种方式: 1.全部放入一个Dockerfile 一种方式是将所有的构建过程全都包含在一个Dockerfile ...
- Windows 怎么知道我已经连接到互联网而不是局域网? 原来当中大有文章!
Windows 怎么知道我已经连接到互联网而不是局域网? 原来当中大有文章! 转载 原文章地址:点击 2014-01-09 Windows 怎么知道我已经连接到互联网而不是局域网? 原来当中大有文章! ...
- [CSP-J2019 江西] 道路拆除 题解
发现大家都是将路径拆成三条链来做,这里提供一种暴力的乱搞方法. 思路 看到这一道题的第一想法就是跑最短路.可是仔细想想就发现,由于重合的路径只算一遍,所以导致两条最短路不一定是最优解. 接着,看到数据 ...