Jump to content

Examples for Math.random() maybe misleading!?


Stickler999

Recommended Posts

https://www.w3schools.com/jsref/jsref_random.asp

There it says:
 

Quote

Return a random number between 1 and 10


Math.floor((Math.random() * 10) + 1);

 

But with .floor() the result "10" is very unlikely, isn't it?

I would prefer .round()

Math.round(Math.random()*10+1);

 

Link to comment
Share on other sites

The "+1" makes it as likely as the other numbers.

If you use Math.round(), what you get is that numbers on both ends have only half the probability as the numbers in the middle, and in your example 11 would be the number at the end.

Link to comment
Share on other sites

13 hours ago, Ingolme said:

The "+1" makes it as likely as the other numbers.

If you use Math.round(), what you get is that numbers on both ends have only half the probability as the numbers in the middle, and in your example 11 would be the number at the end.

I don't agree with you. E.g. you want "0" or "1" as random numbers. According to the example at the reference page it should work like this:

console.log(Math.floor(Math.random()*1+0));

But the result is always "0". This code gives "0" or "1":

console.log(Math.round(Math.random()*1+0));

I'm not clear if

console.log(Math.floor(Math.random()*2+0));

gives a better "randomnes" for 0 oder 1? Any Mathematicians here?

Edited by Stickler999
Link to comment
Share on other sites

So, I implemented a small testcode and found out, that we both were wrong 😉

For two integers "min" and "max" (including both) I get a good eval distribution of integer random numbers with this function:
 

Math.floor(Math.random()*(max+1)+min);

Mozilla docs show a slightly different variant:
 

Math.floor(Math.random() * (max - min + 1)) + min;

which seams to give the same results.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

Link to comment
Share on other sites

Math.random() * 10 gives you a random number in the interval between 0 and 10. If you floor it, 10 will always be left out because random() never returns 1, leaving it at an even distribution between 0 and 9. If you add 1 to that the end result is a number between 1 and 10, so Math.floor(Math.random() * 10 + 1) is a valid way to fairly choose a number between 1 and 10. This excludes zero, if you want zero then you will have to include 11 in your equation and not add the "+1"

Mozilla's example is just a more general solution for if you want control over the range, but if you replace max with 10 and min with 1 the formula still simplifies down to Math.floor(Math.random() * 10 + 1).

I mentioned the problem with round earlier, but a graphic representation should make it clear. The distribution of the numbers on the number line for round is unfair.

              0       1       2       3       4       5       6       7       8       9       10
Number line:  |-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|
              |                                                                               |
Math.round()  |-0-|---1---|---2---|---3---|---4---|---5---|---6---|---7---|---8---|---9---|-10|
              |                                                                               |
Math.floor()  |---0---|---1---|---2---|---3---|---4---|---5---|---6---|---7---|---8---|---9---|

As seen above, the amount of the number line dedicated 0 and 10 is half of the other numbers and there are 11 possible output values for the function when you use round().

The code Math.floor(Math.random()*(max+1)+min), will not give desired results if you plug in 10 and 1. If you plug in those numbers, your formula simplifies to Math.floor(Math.random() * 11 + 1) which will give you random integers between 1 and 11.

In summary, I believe that the W3Schools example is not misleading and is, actually, the only possible way to generate evenly distributed random integers between 1 and 10 in Javascript. Any other code that does that will end up simplifying to that same expression.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...