-
Notifications
You must be signed in to change notification settings - Fork 22.5k
-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Large bounds are not required to cause rounding #35933
Comments
This is because console.log(0.9999999999999999); // 0.9999999999999999
console.log(1.9999999999999999); // 2
console.log(1.9999999999999998); // 1.9999999999999998
Or the function could be written as: const almost_1 = 0.9999999999999999;
Math.random = () => almost_1;
// Example from page, supposedly always returns numbers less than max
function getRandomArbitrary(min, max) {
return (Math.random() - 0.0000000000000001) * (max - min) + min;
}
console.log(getRandomArbitrary(1, 3)); // Logs 2.9999999999999996 |
Well, not quite. As the bounds get larger, the more common it becomes for a rounding error to cause the upper bound to be reached. I'd guess the probability doubles as the bounds doubles.
No, that won't work if Math.random returns 0: Math.random = () => 0;
// Adjusted example, I disagree with
function getRandomArbitrary(min, max) {
return (Math.random() - 0.0000000000000001) * (max - min) + min;
}
console.log(getRandomArbitrary(1, 3)); // Logs 0.9999999999999998 This might be better: Math.random = () => 0.9999999999999999;
// Adjusted example, with special check
function getRandomArbitrary(min, max) {
const result = Math.random() * (max - min) + min;
if (result >= max) return min; // Handle floating point rounding error
return result;
}
console.log(getRandomArbitrary(1, 3)); // Logs 1 now. |
You are right. My point is that the possibility exists across the range, not just with larger bounds.
Returning How about the following? function getRandomArbitrary(min, max) {
let result = max;
while(result >= max) {
result = Math.random() * (max - min) + min;
}
return result;
} Fixing the code blocks would be better than explaining the rounding error cases. |
That's true. We probably shouldn't be too specific since I don't think we really know.
That might be true.
Good in theory, but it would be nicer if it would handle min >= max gracefully. If a beginner tries the wrong way around they would get an infinite loop.
Hmm, I disagree. I think accurate documentation is more important. Example code is nice and useful, but not required. |
MDN URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
What specific section or headline is this issue about?
Examples
What information was incorrect, unhelpful, or incomplete?
"If extremely large bounds are chosen (2**53 or higher), it's possible in extremely rare cases to reach the usually-excluded upper bound."
What did you expect to see?
"It's possible in extremely rare cases to reach the usually-excluded upper bound, especially if large bounds are chosen."
Do you have any supporting links, references, or citations?
No response
Do you have anything more you want to share?
MDN metadata
Page report details
en-us/web/javascript/reference/global_objects/math/random
The text was updated successfully, but these errors were encountered: