mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.22k stars 22.5k forks source link

Random-Max wrong in example #36912

Open Martin-Tabya opened 8 hours ago

Martin-Tabya commented 8 hours ago

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?

First Example "Try It"

What information was incorrect, unhelpful, or incomplete?

In the "Try It" sandbox (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#try_it) the code is off by 1. The generated integers in the first output-line will be generated between 0 and 2 while we passed a "3" as param for the "max value".

What did you expect to see?

If we call getRandomInt(3) i expect to see values between 0 and 3. Same for the example line with getRandomInt(0), in this case there will be always a 0 returned.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

As this example code will be used my thousands or more of users when they search for "js random", this will provide them coded with an off-by-1 mistake. Please make sure they fully understand that it's not the "maximum value" they are passing as param. I suggest changing the code example so that it really takes the passed param as max value UNCLUDING the value itself, because that is the mostly wanted usecase.

MDN metadata

Page report details * Folder: `en-us/web/javascript/reference/global_objects/math/random` * MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/javascript/reference/global_objects/math/random/index.md * Last commit: https://github.com/mdn/content/commit/0aba9992d39210d5b8a8b204c5baafd3b31c4e25 * Document last modified: 2024-09-19T21:56:30.000Z