Python生成随机数的方法详解(最全)
作者:梦想橡皮擦 时间:2023-05-04 12:36:08
python生成随机数都有哪些办法呢
使用 random 模块:random模块是python内置的模块,使用方法如random.randint()生成一个随机整数。
使用 NumPy 库:NumPy 是一个强大的数值计算库,它提供了生成随机数的功能,例如numpy.random.randint()生成一个随机整数。
使用 secrets 模块:secrets模块是python3.6版本引入的新模块,专门用于生成安全随机数,例如secrets.randbelow()生成一个随机整数。
使用 random.org 网站:random.org 网站可以生成真正的随机数,可以使用Python的requests模块从该网站获取随机数。
使用 random.choices()方法: random.choices()方法可以从一个序列中随机选择多个元素。
使用 random 模块
使用 random 模块的 randint() 函数生成随机整数:
import random
#生成随机整数
random_int = random.randint(1,100)
print(random_int)
也可以使用 random 模块的 random() 函数生成随机小数:
import random
#生成随机小数
random_float = random.random()
print(random_float)
可以使用 random 模块的 choice() 函数从列表中随机选取一个元素:
import random
#随机从列表中选取一个元素
random_list = [1, 2, 3, 4, 5]
random_element = random.choice(random_list)
print(random_element)
还可以使用 random 模块的 sample() 函数随机从列表中选取多个不重复的元素:
import random
#随机从列表中选取多个不重复的元素
random_list = [1, 2, 3, 4, 5]
random_elements = random.sample(random_list, 3)
print(random_elements)
最后,还可以使用 random 模块的 shuffle() 函数随机打乱列表的顺序:
import random
#随机打乱列表的顺序
random_list = [1, 2, 3, 4, 5]
random.shuffle(random_list)
print(random_list)
使用 NumPy 库
代码如下所示:
import numpy as np
# 生成一个随机整数
rand_int = np.random.randint(low=0, high=100)
print("随机整数:", rand_int)
# 生成一个包含随机整数的数组
rand_array = np.random.randint(low=0, high=100, size=(5,))
print("随机整数数组:", rand_array)
使用 NumPy 生成随机数的优势在于,它提供了丰富的随机数生成功能,可以生成各种形状和分布的随机数组,在数据分析、机器学习等领域有广泛应用。
使用 secrets 模块
secrets模块提供了更安全的方法,且易于使用。
使用secrets.randbelow()可生成一个随机整数,而无需关心随机数的范围。
它可以保证生成的随机数具有足够的随机性,适用于密码生成、加密等安全相关的场景。
详细代码可以参考以下例子:
import secrets
random_number = secrets.randbelow(100)
print(random_number)
使用 random.org 网站
random.org 网站是一个生成真正随机数的网站,你可以使用Python的requests模块从该网站获取随机数,使用方法如下:
import requests
import json
# 请求随机数的API
url = 'https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new'
# 发送请求并获取随机数
response = requests.get(url)
random_number = response.text
# 转换为整数并打印
random_number = int(random_number)
print(random_number)
这是一个使用requests从random.org获取随机数的简单示例。你可以根据需求自定义生成随机数的数量、最小值和最大值。
使用 random.choices()方法
random.choices() 方法可以从指定的序列中随机选择多个元素,并以列表的形式返回选择的结果。
该方法的语法如下:
random.choices(sequence, weights=None, *, cum_weights=None, k=1)
详细参数如下:
sequence:要从中选择的元素的序列
weights:序列中各元素的概率分布,可以为 None(默认值,表示所有元素的概率相等)
cum_weights:weights 参数的累积概率分布
k:选择的元素个数
来源:https://blog.csdn.net/hihell/article/details/128905722
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简单聊聊Golang中defer预计算参数
Python JSON编解码方式原理详解
![](https://img.aspxhome.com/file/2023/2/97322_0s.png)
Python图像的增强处理操作示例【基于ImageEnhance类】
![](https://img.aspxhome.com/file/2023/3/127883_0s.jpg)
Python容器类型公共方法总结
python实现人机对战的五子棋游戏
![](https://img.aspxhome.com/file/2023/0/65910_0s.jpg)
Golang中panic的异常处理
Python3.x和Python2.x的区别介绍
Python+tkinter使用80行代码实现一个计算器实例
![](https://img.aspxhome.com/file/2023/5/108045_0s.png)
python基础教程之常用运算符
Matplotlib实现各种条形图绘制
![](https://img.aspxhome.com/file/2023/0/118410_0s.png)
ASP如何跳出本次进入下一次循环
21行Python代码实现拼写检查器
Django操作cookie的实现
![](https://img.aspxhome.com/file/2023/1/77011_0s.jpg)
解决selenium模块利用performance获取network日志请求报错的问题(亲测有效)
![](https://img.aspxhome.com/file/2023/9/86549_0s.jpg)
JavaScript 与 ActionScript 3.0 交互的一些问题
简单三步轻松实现ORACLE字段自增
VS2019 自定义项目模板的实现方法
![](https://img.aspxhome.com/file/2023/6/112086_0s.png)
Python fire模块(最简化命令行生成工具)的使用教程详解
![](https://img.aspxhome.com/file/2023/0/105280_0s.png)
详解使用Python处理文件目录的相关方法
python基于pygame实现响应游戏中事件的方法(附源码)
![](https://img.aspxhome.com/file/2023/0/124910_0s.png)