如何在这些点之间生成随机双精度?
37.807614 to 37.786996
随机生成的 double 必须具有与上述相同的精度(位数)。
例如,37.792242 会很好,而 37.7823423425 会很糟糕。
37.807614 to 37.786996
The randomly generated double must have the same precision (num of digits) as those above.
For example, 37.792242 would be good, whereas 37.7823423425 would be bad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不过,由于 IEEE 754 不准确,您偶尔会得到以
...0001
或...9999
结尾的数字。You will occasionally get numbers that end with
...0001
or...9999
or so due to IEEE 754 inaccuracies though.不确定我是否遗漏了一些东西,但为了避免舍入,并且由于您想要精确的精度,所以使用整数然后通过除法转换为浮点不是最简单的吗?即
offset = float(random.randint(0,20618))
数字 = (偏移量 + 37786996.0) / 1000000.0
Not sure if I'm missing something, but to avoid rounding, and since you want exactly that precision, isn't it easiest to work in integers and then convert to floating point by division? I.e.
offset = float(random.randint(0,20618))
num = (offset + 37786996.0) / 1000000.0