python - 你会如何在 Python 的一个范围内随机改变一个浮点数?
问题描述
下面是一个名为“inputfile”的输入文件中的浮点列表,我想遍历每个浮点并将其随机化 ± 0.100。我该怎么做呢?
0.08777767595196
0.41688405291929
1.90493522025702
-0.44512262940079
-1.68572227053594
-0.19769851139757
0.61588125474274
2.98863319423069
-0.78312326907806
-2.73208403405514
-0.36006068363418
解决方案
input = open("inputfile", "r").readlines()
import random
noise = 0.1
output = [float(element) + random.random() * noise * 2 - noise for element in input ]
print(output )
output 是您要查找的列表并输入您的输入列表。
如果您的文件太大而无法存储两个数组,您可以直接修改输入文件,如下所示:
noise = 0.1
for i in range(len(input)):
input[i] = float(input[i]) + random.random() * noise * 2 - noise
推荐阅读
- javascript - 创建具有多个值的 JSON
- c# - 如何模拟 autofac 类型注册?
- android - 在 PWA 中上传图像/文件
- javascript - 在 pre-ES6 JavaScript 中使用通配符比较字符串
- capl - 如何使用 CAPL 脚本或任何其他可能的方式测试 CanTp 协议中的 N_As、N_Ar 超时参数?
- .net - .Net 框架 4.7.2 中 HttpCookie.SameSite 选项的构建失败
- spring-hateoas - 带有 Traverson 客户端和 java.time.Instant 的 Spring HATEOAS
- iis - 具有多个规则的 IIS 反向代理
- mysql - Mysql - 选择具有链接值的行
- python-3.x - 在跳过某些对象时获取列表中的不同部分