This diagram shows you which floats map to which integer with different functions. As you can see, in the case of Math.round(), 0 has only half the length of every other number, so it would appear half as often. Same goes for 10 at the end of the interval:
While 10 would appear in the resulting histogram, its frequency would be about half the frequency of every other non-zero integer.
FYI, Math.round() is just:
constround=n=>Math.floor(n+0.5);
You're effectively just shifting the mapping from floats to integers, but you're not making room for the max number. To have even distribution, you must have all intervals of equal length, and all the intervals covered by the scaled random number interval.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In that case,
0
and10
would appear with about 50% the frequency of every other element.If you imagine the number line after multiplying by 10, you'll understand what I mean:
This diagram shows you which floats map to which integer with different functions. As you can see, in the case of
Math.round()
,0
has only half the length of every other number, so it would appear half as often. Same goes for10
at the end of the interval:While
10
would appear in the resulting histogram, its frequency would be about half the frequency of every other non-zero integer.FYI, Math.round() is just:
You're effectively just shifting the mapping from floats to integers, but you're not making room for the
max
number. To have even distribution, you must have all intervals of equal length, and all the intervals covered by the scaled random number interval.