admin管理员组文章数量:1296858
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?
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.
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?
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?
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.
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?
Share Improve this question asked Aug 30, 2018 at 5:05 Isaac ReefmanIsaac Reefman 5971 gold badge10 silver badges29 bronze badges 11-
Why does returning zero from
Math.random
matter to you? – Tim Biegeleisen Commented Aug 30, 2018 at 5:07 - 1 @TimBiegeleisen As I mentioned in the question, it has implications for using it to generate random integers. I could go with the mon way of doing things (using .floor...+1 instead of .ceil) but I'd like to know if it's actually necessary to prevent inaccuracies (or code potentially breaking every 1 in a billiard iterations). Plus it may have implications on the actual randomness of this method. – Isaac Reefman Commented Aug 30, 2018 at 5:10
- A possible hackish fix: Check for exact equality to zero, and, if true, then add whatever the smallest possible number is in JavaScript. This would have almost a negligible effect on the distribution, I think, but would avoid the zero problem. – Tim Biegeleisen Commented Aug 30, 2018 at 5:12
-
1
@TimBiegeleisen I mean, yeah, but adding in an
if (result == 0){result =+ Number.MIN_VALUE}
seems more unwieldy than just going.floor...+1
. Interesting to note though. – Isaac Reefman Commented Aug 30, 2018 at 5:16 - 1 Significant digits – Jaromanda X Commented Aug 30, 2018 at 8:17
3 Answers
Reset to default 10JavaScript is a dialect of ECMAScript. The ECMAScript-262 standard fails to specify Math.random
precisely. The relevant clause says:
Math.random ( )
Returns a Number value with positive sign, greater than or equal to +0
本文标签: javascriptWhat are the chances of Mathrandom returning 0Stack Overflow
版权声明:本文标题:javascript - What are the chances of Math.random returning 0? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639568a2389836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论