生成介于0.1和1.0之间的随机数. Python [英] Generate random number between 0.1 and 1.0. Python

查看:195
本文介绍了生成介于0.1和1.0之间的随机数. Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个介于0.1和1.0之间的随机数. 我们不能使用rand.randint,因为它返回整数. 我们也尝试了random.uniform(0.1,1.0),但是它返回的值> = 0.1且< 1.0,我们不能使用它,因为我们的搜索还包括1.0.

I'm trying to generate a random number between 0.1 and 1.0. We can't use rand.randint because it returns integers. We have also tried random.uniform(0.1,1.0), but it returns a value >= 0.1 and < 1.0, we can't use this, because our search includes also 1.0.

有人对这个问题有想法吗?

Does somebody else have an idea for this problem?

推荐答案

您希望随机数有多准确"?如果您对10个十进制数字感到满意,则可以将random.uniform(0.1, 1.0)舍入为10个数字.这样,您将同时包含0.11.0:

How "accurate" do you want your random numbers? If you're happy with, say, 10 decimal digits, you can just round random.uniform(0.1, 1.0) to 10 digits. That way you will include both 0.1 and 1.0:

round(random.uniform(0.1, 1.0), 10)

确切地说,与之间的任何其他数字相比,0.11.0的概率只有一半,并且,当然,您会丢失所有仅在10位数字之后才不同的随机数.

To be precise, 0.1 and 1.0 will have only half of the probability compared to any other number in between and, of course, you loose all random numbers that differ only after 10 digits.

这篇关于生成介于0.1和1.0之间的随机数. Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆