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
 |  Show 6 more ments

3 Answers 3

Reset to default 10

JavaScript 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