是否可能Math.random()=== Math.random() [英] Is it possible for Math.random() === Math.random()

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

问题描述

在JavaScript中,此表达式是否会在任何浏览器中评估为true?为什么或为什么不呢?

In JavaScript, will this expression ever evaluate to true in any browser? Why or why not?

  Math.random() === Math.random()

注意:请按字面意思执行上述代码。我不是在问Math.random是否会生成重复值。

Note: Please do take the above code literally. I'm not asking if Math.random will ever generate duplicate values.

注2:没有猴子补丁

这个问题是关于Math.random()的内部实现,而不是关于随机数的性质。

This question is about the internal Implementation of Math.random(), not about the nature of random numbers.

推荐答案


表达式 Math.random()=== Math.random()是否会在任何浏览器中评估为true?

Will the expression Math.random() === Math.random() ever evaluate to true in any browser?

是的,它可能已经发生了。

Yes, and it's likely to have happened already.


这个问题是关于的内部实现Math.random()

嗯,有没有在单个实现中,每个javascript引擎都会实现自己的实现。这是随机性不能 值得信赖,但常见引擎确实使用了31,32,48或52位熵。

这意味着从两个连续获得相同值的概率调用(或者,来自任何两个调用)是2 -31 ,2 -32 等。这听起来不多,但是2 31 只是关于互联网用户的数量...

Well, there isn't a single implementation, every javascript engine does implement its own one. It's randomness cannot be trusted, but common engines do/did use 31, 32, 48 or 52 bits of entropy.
This means that the probability of getting the same value from two consecutive calls (or, from any two calls) is 2-31, 2-32 etc. That doesn't sound much, but 231 is just about the number of internet users…

哦,当然总是像这样的错误 ...

Oh, and of course there are always bugs like this one…

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

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