python中的random() [英] random() in python

查看:79
本文介绍了python中的random()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,函数random()均匀地在半开放范围[0.0,1.0)中生成一个随机浮点数.原则上,它可以产生0.0(即零)和1.0(即1)吗?实际情况是什么?

In python the function random() generates a random float uniformly in the semi-open range [0.0, 1.0). In principle can it ever generate 0.0 (i.e. zero) and 1.0 (i.e. unity)? What is the scenario in practicality?

推荐答案

0.0可以生成; 1.0不能(因为它不在范围内,因此)[相对).

0.0 can be generated; 1.0 cannot (since it isn't within the range, hence the ) as opposed to [).

生成0.0的概率等于生成该范围内任何其他数字的概率,即1/X,其中X是不同可能结果的数量.对于标准的无符号双精度浮点,这通常意味着53位分数部分,对于2 ^ 53种可能的组合,导致产生(c ^> 1)的机会是((2 ^ 53)).

The probability of generating 0.0 is equal to the probability of generating any other number within that range, namely, 1/X where X is the number of different possible results. For a standard unsigned double-precision floating point, this usually means 53 bits of fractional component, for 2^53 possible combinations, leading to a 1/(2^53) chance of generating exactly 0.0.

因此,尽管它有可能完全返回0.0,但您不太可能很快看到它-但也不太可能看到您可能预先选择的任何其他特定值.

So while it's possible for it to return exactly 0.0, it's unlikely that you'll see it any time soon - but it's just as unlikely that you'd see exactly any other particular value you might choose in advance.

这篇关于python中的random()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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