English 中文(简体)
Python标准库中的random.random()的范围
原标题:Range of python s random.random() from the standard library

Python 的 random.random() 函数是否会返回 1.0 或者只会返回到 0.9999..?

最佳回答

文档在这里:http://docs.python.org/library/random.html

...random(), which generates a random float uniformly in the semi-open range [0.0, 1.0).

因此,收益价值将大于或等于0,低于1.0。

问题回答
>>> help(random.random)
Help on built-in function random:

random(...)
    random() -> x in the interval [0, 1).

这意味着1被排除在外。

其他答复已经澄清说,其中1个没有列入范围,但出于好奇,我决定审视来文方,以准确了解如何计算。

CPython源代码可以在这里找到。

/* random_random is the function named genrand_res53 in the original code;
 * generates a random number on [0,1) with 53-bit resolution; note that
 * 9007199254740992 == 2**53; I assume they re spelling "/2**53" as
 * multiply-by-reciprocal in the (likely vain) hope that the compiler will
 * optimize the division away at compile-time.  67108864 is 2**26.  In
 * effect, a contains 27 random bits shifted left 26, and b fills in the
 * lower 26 bits of the 53-bit numerator.
 * The orginal code credited Isaku Wada for this algorithm, 2002/01/09.
 */
static PyObject *
random_random(RandomObject *self)
{
    unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
    return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
}

因此,该函数有效地生成 m/2^53,其中 0 <= m < 2^53 是一个整数。由于浮点数通常具有53位精度,这意味着在范围 [1/2, 1) 内,生成了所有可能的浮点数。对于接近0的值,为提高效率,它跳过了一些可能的浮点值,但生成的数字在该范围内均匀分布。 由 random.random 生成的最大可能数字是精确的。

零点九九九九九九九九九九九九九九九九八八九七七六九七五三七四八三四五九五七六三六八三三一九零九一七九六八。

Python的random.random函数返回小于但不等于1的数字。

然而,它可以返回0

从Antimony的答案中的代码中可以很容易地看到,在涉及没有用f注释的常量的计算中,随机数函数random.random()在至少有53位尾数的平台上从不返回完全等于 1.0 的结果。这就是IEEE 754规定的精度,也是今天的标准。

然而,在精度较低的平台上,例如如果Python编译为使用嵌入式平台的-fsingle-precision-constant,将b添加到a * 67108864.0可能会导致舍入到2 ^ 53如果b足够接近2 ^ 26,并且这意味着将返回1.0,请注意,这发生的不管Python的PyFloat_FromDouble函数使用何种精度。

一种测试方法是检查几百个随机数中的第53位是否为1。如果至少出现了一次,证明精度足够,并且正常。如果没有,最有可能的解释是四舍五入导致random.random()可能会返回1.0。当然,你可能只是运气不佳。你可以通过测试更多的数字来提高确定性。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签