Stickler999 0 Posted January 28 Report Share Posted January 28 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); Quote Link to post Share on other sites

Ingolme 1,018 Posted January 29 Report Share Posted January 29 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. Quote Link to post Share on other sites

Stickler999 0 Posted January 29 Author Report Share Posted January 29 (edited) 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 January 29 by Stickler999 Quote Link to post Share on other sites

Stickler999 0 Posted January 29 Author Report Share Posted January 29 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 Quote Link to post Share on other sites

Ingolme 1,018 Posted January 30 Report Share Posted January 30 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. Quote Link to post Share on other sites

## Recommended Posts

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.