Math.floor(Math.random() * 10)gives a fairly uniform distribution, but Math.round(Math.random() * 10)not.
Math.floor () returns 0 for any value in the range [0, 1) (1 exception), 1 for any value in the range [1, 2], etc.
So, if we have an equal chance of getting a number in one of these ranges, we get an equal distribution of 0 and 1.
Math.round (), however, returns 0 for values ββbelow 0.5, 1 for values ββbelow 1.5, etc.
, 0, 0 0,5 0.
βββββββββββ¦ββββββββββ¦ββββββββββ
β Range β floor() β round() β
β ββββββββββ¬ββββββββββ¬ββββββββββ£
β [0, 1) β 0 β 0 or 1 β
β [1, 2) β 1 β 1 or 2 β
β [2, 3) β 2 β 2 or 3 β
β ... β ... β ... β
β [9, 10) β 9 β 9 or 10 β
βββββββββββ©ββββββββββ©ββββββββββ