Math.random返回0的几率是多少? [英] What are the chances of Math.random returning 0?

查看:814
本文介绍了Math.random返回0的几率是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像这个问题的提问者一样,我想知道为什么为什么Math.ceil(Math.random() * 10)Math.floor(Math.random() * 10) + 1不受欢迎?发现这是因为Math.random具有很小的(但相关的)准确返回0的机会.但是有多小?

Like the asker of this question, I was wondering why Math.ceil(Math.random() * 10) was not preferred over Math.floor(Math.random() * 10) + 1, and found that it was because Math.random has a tiny (but relevant) chance of returning 0 exactly. But how tiny?

进一步的研究告诉我该随机数精确到小数点后16位...嗯,差不多.这是我很好奇的那种".

Further research told me that this random number is accurate to 16 decimal places... well, sort of. And it's the "sort of" that I'm curious about.

我知道浮点数的工作方式不同于小数.但是我在细节上挣扎.如果数字是严格的十进制值,我相信机率是十分之一(在美国系统中为十万亿)-1:10 16 .

I understand that floating point numbers work differently to decimals. I struggle with the specifics though. If the number were a strict decimal value, I believe the chances would be one in ten billiard (or ten quadrillion, in the American system) - 1:1016.

这是正确的,还是我搞砸了,还是浮点数有所作为?

Is this correct, or have I messed up, or does the floating point thing make a difference?

推荐答案

JavaScript是ECMAScript的方言. ECMAScript-262标准无法精确指定Math.random .在第20.2.2.7条中,它说:

JavaScript is a dialect of ECMAScript. The ECMAScript-262 standard fails to specify Math.random precisely. In clause 20.2.2.7, it says:

20.2.2.27 Math.random()

使用依赖于实现的算法或策略,返回一个正号(大于或等于0但小于1)的Number值,该值是在该范围内以近似均匀的分布随机或伪随机选择的.此函数不带任何参数. 为不同领域创建的每个 Math.random 函数必须从连续调用中产生不同的值序列.

Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments. Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls.

在没有完整规范的情况下,无法对Math.random返回零的概率做出明确的说明.每个ECMAScript实现都可以选择不同的算法,而不必提供真正统一的分布.

In the absence of a complete specification, no definitive statement can be made about the probability of Math.random returning zero. Each ECMAScript implementation may choose a different algorithm and need not provide a truly uniform distribution.

ECMAScript的Number类型使用IEEE-754基本的64位二进制浮点格式.在此格式中,数字的有效位数(小数部分)具有53位.每个浮点数的格式为 s f •2 e ,其中 s (用于符号)是+1或-1, f (用于分数)是有效位数,并且是[0,2 53 ]中的整数,而 e (对于指数)是[−1074,971]中的整数.如果设置了 f 的高位,则该数字被归一化(因此 f 位于[2 52 ,2 53 )).由于在此答案中无需考虑负数,因此让 s 对该答案的其余部分隐式+1.

ECMAScript uses the IEEE-754 basic 64-bit binary floating-point format for its Number type. In this format, the significand (fraction portion) of the number has 53 bits. Every floating-point number has the form sf • 2e, where s (for sign) is +1 or −1, f (for fraction) is the significand and is an integer in [0, 253), and e (for exponent) is an integer in [−1074, 971]. The number is said to be normalized if the high bit of f is set (so f is in [252, 253)). Since negative numbers are not a concern in this answer, let s be implicitly +1 for the rest of this answer.

在[0,1)中分配随机数的一个问题是可代表的值不是均匀分布的.在[½,1)中有2个 52 可表示的值-所有在[2 52 ,2 53 )和 e = −53.并且在[¼,½)中有相同数量的值-在[2 52 ,2 53 )和 e = −54.由于在此间隔中有相同数量的数字,但间隔是长度的一半,因此数字之间的间隔更近.同样,在[⅛,¼)中,间距再次减半.这一直持续到指数达到−1074为止,此时正常数以 f = 2 52 结尾.小于那个的数被认为是次正规的(或零),其中 f 在[0,2 52 )中, e = −1074 ,并且它们之间的间距均匀.

One issue with distributing random numbers in [0, 1) is that the representable values are not evenly spaced. There are 252 representable values in [½, 1)—all those with f in [252, 253) and e = −53. And there are the same number of values in [¼, ½)—all those with f in [252, 253) and e = −54. Since there are the same number of numbers in this interval but the interval is half as long, the numbers are more closely spaced. Similarly, in [⅛, ¼), the spacing halves again. This continues until the exponent reaches −1074, at which point the normal numbers end with f = 252. The numbers smaller than that are said to be subnormal (or zero), with f in [0, 252) and e = −1074, and they are evenly spaced.

关于如何分配Math.random的数字​​的一种选择是仅使用一组均匀间隔的数字 f •2 −53 用于 f 中的[0,2 53 ).这将使用[1/2,1)中所有可表示的值,但仅使用[1/4,½)中的一半,[1/3,¼)中的四分之一,依此类推.这很简单,并且避免了分布中的一些奇怪之处.如果正确实施,则产生零的概率为2 53 中的一.

One choice about how to distribute the numbers for Math.random is to use only the set of evenly spaced numbers f • 2−53 for f in [0, 253). This uses all the representable values in [½, 1), but only half the values in [¼, ½), one-fourth the values in [⅛, ¼), and so on. This is simple and avoids some oddities in the distribution. If implemented correctly, the probability zero is produced is one in 253.

另一种选择是使用[0,1)中的所有可表示值,每个值的概率与从它到下一个更高的可表示值的距离成正比.因此,将以概率1/2 53 选择[1/2,1)中的每个可表示数字,以概率<1/2 54 ,则以概率1/2 55 选择[⅛,¼)中每个可表示的数字,依此类推.在浮点格式更精细的情况下,此分布近似于实数和提供者更精细的精度上的均匀分布.如果正确实施,则产生零的概率为2 1074 中的一.

Another choice is to use all the representable values in [0, 1), each with probability proportional to the distance from it to the next higher representable value. Thus, each representable number in [½, 1) would be chosen with probability 1/253, each representable number in [¼, ½) would be chosen with probability 1/254, each representable number in [⅛, ¼) would be chosen with probability 1/255, and so on. This distribution approximates a uniform distribution on the reals and providers finer precision where the floating-point format is finer. If implemented correctly, the probability zero is produced is one in 21074.

另一种选择是使用[0,1)中的所有可表示值,每个值与段的长度成正比,其中可表示值是该段中所有实数的最接近可表示值.我将省略对这种分布的一些细节的讨论,只是说它模仿一个结果,方法是选择一个具有均匀分布的实数,然后使用四舍五入关系"将其四舍五入为可表示的值. .如果正确实施,则产生零的概率为2 1075 中的一. (这种分布的一个问题是,在[0,1]上的实数上的均匀分布有时会产生一个非常接近1的数字,因此舍入会产生1.这然后要求允许Math.random返回1或可以通过某种方式来伪造分布,也许是通过返回下一个较低的可表示值而不是1来实现.)

Another choice is to use all the representable values in [0, 1), each with probability proportional to the length of the segment in which the representable value is the nearest representable value of all the real numbers in the segment. I will omit discussion of some details of this distribution except to say it mimics the results one would get by choosing a real number with uniform distribution and then rounding it to a representable value using the round-to-nearest-ties-to-even rule. If implemented correctly, the probability zero is produced is one in 21075. (One problem with this distribution is that a uniform distribution over the reals in [0, 1) will sometimes produce a number so close to 1 that rounding produces 1. This then requires either that Math.random be allowed to return 1 or that the distribution be fudged in some way, perhaps by returning the next lower representable value instead of 1.)

我将注意到ECMAScript规范过于宽松,以至于人们可能会断言Math.random可以以相等的概率为每个可表示的值分配数字,而忽略它们之间的间隔.这根本不会模仿真实数字的均匀分布,我希望很少有人会喜欢它.但是,如果实施,则返回零的概率为1021•2 52 中的1,因为存在2 52 归一化的数字,指数从-53到-1074(1020值 e )和2 52 次正规或零数.

I will note that the ECMAScript specification is sufficiently lax that one might assert that Math.random may distribute the numbers with equal probability for each representable value, ignoring the spacing between them. This would not mimic a uniform distribution over the real numbers at all, and I expect very few people would favor it. However, if implemented, the probability zero is returned is one in 1021 • 252, because there are 252 normalized numbers with exponents from −53 to −1074 (1020 values of e), and 252 subnormal or zero numbers.

这篇关于Math.random返回0的几率是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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