- Python 基础
- Python - 首页
- Python - 概述
- Python - 历史
- Python - 特性
- Python vs C++
- Python - Hello World 程序
- Python - 应用领域
- Python - 解释器
- Python - 环境设置
- Python - 虚拟环境
- Python - 基本语法
- Python - 变量
- Python - 数据类型
- Python - 类型转换
- Python - Unicode 系统
- Python - 字面量
- Python - 运算符
- Python - 算术运算符
- Python - 比较运算符
- Python - 赋值运算符
- Python - 逻辑运算符
- Python - 位运算符
- Python - 成员运算符
- Python - 身份运算符
- Python - 运算符优先级
- Python - 注释
- Python - 用户输入
- Python - 数字
- Python - 布尔值
- Python 控制语句
- Python - 控制流
- Python - 决策
- Python - If 语句
- Python - If else
- Python - 嵌套 If
- Python - Match-Case 语句
- Python - 循环
- Python - for 循环
- Python - for-else 循环
- Python - While 循环
- Python - break 语句
- Python - continue 语句
- Python - pass 语句
- Python - 嵌套循环
- Python 函数 & 模块
- Python - 函数
- Python - 默认参数
- Python - 关键字参数
- Python - 仅关键字参数
- Python - 位置参数
- Python - 仅位置参数
- Python - 可变参数
- Python - 变量作用域
- Python - 函数注解
- Python - 模块
- Python - 内置函数
- Python 字符串
- Python - 字符串
- Python - 字符串切片
- Python - 修改字符串
- Python - 字符串连接
- Python - 字符串格式化
- Python - 转义字符
- Python - 字符串方法
- Python - 字符串练习
- Python 列表
- Python - 列表
- Python - 访问列表元素
- Python - 修改列表元素
- Python - 添加列表元素
- Python - 删除列表元素
- Python - 遍历列表
- Python - 列表推导式
- Python - 排序列表
- Python - 复制列表
- Python - 合并列表
- Python - 列表方法
- Python - 列表练习
- Python 元组
- Python - 元组
- Python - 访问元组元素
- Python - 更新元组
- Python - 解包元组
- Python - 遍历元组
- Python - 合并元组
- Python - 元组方法
- Python - 元组练习
- Python 集合
- Python - 集合
- Python - 访问集合元素
- Python - 添加集合元素
- Python - 删除集合元素
- Python - 遍历集合
- Python - 合并集合
- Python - 复制集合
- Python - 集合运算符
- Python - 集合方法
- Python - 集合练习
- Python 字典
- Python - 字典
- Python - 访问字典元素
- Python - 修改字典元素
- Python - 添加字典元素
- Python - 删除字典元素
- Python - 字典视图对象
- Python - 遍历字典
- Python - 复制字典
- Python - 嵌套字典
- Python - 字典方法
- Python - 字典练习
- Python 数组
- Python - 数组
- Python - 访问数组元素
- Python - 添加数组元素
- Python - 删除数组元素
- Python - 遍历数组
- Python - 复制数组
- Python - 反转数组
- Python - 排序数组
- Python - 合并数组
- Python - 数组方法
- Python - 数组练习
- Python 文件处理
- Python - 文件处理
- Python - 写入文件
- Python - 读取文件
- Python - 重命名和删除文件
- Python - 目录
- Python - 文件方法
- Python - OS 文件/目录方法
- Python - OS 路径方法
- 面向对象编程
- Python - OOPs 概念
- Python - 类 & 对象
- Python - 类属性
- Python - 类方法
- Python - 静态方法
- Python - 构造函数
- Python - 访问修饰符
- Python - 继承
- Python - 多态
- Python - 方法重写
- Python - 方法重载
- Python - 动态绑定
- Python - 动态类型
- Python - 抽象
- Python - 封装
- Python - 接口
- Python - 包
- Python - 内部类
- Python - 匿名类和对象
- Python - 单例类
- Python - 包装类
- Python - 枚举
- Python - 反射
- Python 错误 & 异常
- Python - 语法错误
- Python - 异常
- Python - try-except 块
- Python - try-finally 块
- Python - 抛出异常
- Python - 异常链
- Python - 嵌套 try 块
- Python - 用户自定义异常
- Python - 日志记录
- Python - 断言
- Python - 内置异常
- Python 多线程
- Python - 多线程
- Python - 线程生命周期
- Python - 创建线程
- Python - 启动线程
- Python - 合并线程
- Python - 线程命名
- Python - 线程调度
- Python - 线程池
- Python - 主线程
- Python - 线程优先级
- Python - 守护线程
- Python - 线程同步
- Python 同步
- Python - 线程间通信
- Python - 线程死锁
- Python - 中断线程
- Python 网络
- Python - 网络
- Python - 套接字编程
- Python - URL 处理
- Python - 泛型
- Python 库
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 杂项
- Python - 日期 & 时间
- Python - 数学
- Python - 迭代器
- Python - 生成器
- Python - 闭包
- Python - 装饰器
- Python - 递归
- Python - 正则表达式
- Python - PIP
- Python - 数据库访问
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 输出格式化
- Python - 性能测量
- Python - 数据压缩
- Python - CGI 编程
- Python - XML 处理
- Python - GUI 编程
- Python - 命令行参数
- Python - 文档字符串
- Python - JSON
- Python - 发送邮件
- Python - 扩展
- Python - 工具/实用程序
- Python - GUIs
- Python 高级概念
- Python - 抽象基类
- Python - 自定义异常
- Python - 高阶函数
- Python - 对象内部
- Python - 内存管理
- Python - 元类
- Python - 使用元类进行元编程
- Python - 模拟和存根
- Python - 猴子补丁
- Python - 信号处理
- Python - 类型提示
- Python - 自动化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 协程
- Python - 描述符
- Python - 诊断和修复内存泄漏
- Python - 不可变数据结构
- Python 有用资源
- Python - 问答
- Python - 在线测验
- Python - 快速指南
- Python - 参考
- Python - 速查表
- Python - 项目
- Python - 有用资源
- Python - 讨论
- Python 编译器
- NumPy 编译器
- Matplotlib 编译器
- SciPy 编译器
Python random.seed() 方法
Python random.seed() 方法用于生成随机数。它借助伪随机数生成器,通过对给定值执行某些操作来实现。random 模块使用种子值作为基础在 Python 中创建随机数。
伪随机数生成器在播种时获取其第一个“前一个”值。对于特定的随机数生成器,每个种子值将对应于生成的一系列值。换句话说,如果您两次使用相同的种子,您将两次获得相同的数字集。
注意 - 此函数无法直接访问,因此我们需要导入 random 模块,然后需要使用 random 静态对象调用此函数。
语法
以下是Python random.seed()方法的语法 -
random.seed(x, version)
参数
x(可选) - 这是下一个随机数的种子。如果省略,则它采用系统时间生成下一个随机数。如果 x 为 None,则默认使用当前系统时间。
version - 这是一个整数值。它描述了如何将参数x转换为整数值。它的默认值为 2。
返回值
此方法不返回值。它仅修复随机数生成。
示例 1
以下示例显示了 Python random.seed() 方法的使用。在这里,我们还使用了另一个称为 random() 的 random 模块方法。它生成位于指定范围“10”内的随机数。
import random
random.seed( 10 )
print ("Random number with seed 10 : ", random.random())
# It will generate same random number
random.seed(10)
print ("Random number with seed 10 : ", random.random())
# It will generate same random number
random.seed(10)
print ("Random number with seed 10 : ", random.random())
当我们运行以上程序时,它会产生以下结果 -
Random number with seed 10 : 0.57140259469 Random number with seed 10 : 0.57140259469 Random number with seed 10 : 0.57140259469
示例 2
如果我们两次使用相同的种子,我们将两次获得相同的随机数集。
在下面给出的示例中,相同的种子值“98”被提供给 random.seed() 方法。因此,获得了相同的随机数。这是因为为随机数生成器指定了第一个前一个值(种子)。因此,在每次运行中,生成器都会生成相同的数字。
# import the random module
import random
# set the seed with the value 98
x = random.seed(98)
# generate the random number between 1-100
print("The random number generated is:", random.random()*100)
# again set the seed with the value of 98
x = random.seed(98)
# generate the random number between 1-100
print("The random number generated is:", random.random()*100)
在执行上述代码时,我们得到以下输出 -
The random number generated is: 35.639820676272215 The random number generated is: 35.639820676272215
示例 3
在这里,我们在调用 random 模块之前传递不同的种子值。因此,我们获得不同的随机数。
# import the random module
import random
# set the seed with the value 98
x = random.seed(30)
# generate the random number between 1-100
print("The random number generated is:", random.random()*100)
# again set the seed with the value of 60
x = random.seed(60)
# generate the random number
print("The random number generated is:", random.random())
以下是上述代码的输出 -
The random number generated is: 53.90815646058106 The random number generated is: 0.3078067547337774
示例 4
以下是使用 for 循环的 seed() 方法的另一个示例。在这里,我们看到重复的输出 4 次。
import random
for i in range(4):
# random number with seed 9
random.seed(9)
for i in range(3):
print('The random number with seed 9 is:',random.random())
print('The random number generated is:')
上述代码的输出如下 -
The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: The random number with seed 9 is: 0.46300735781502145 The random number with seed 9 is: 0.37331193139504204 The random number with seed 9 is: 0.13853941251445523 The random number generated is: