function randomInteger(min, max) { var rand = min + Math.random() * (max - min) rand = Math.round(rand); return rand; }
This feature works. But at the same time it is incorrect: the probability to get the extreme values of min and max will be two times less than any other.
When you run this code multiple times, you will easily notice that 2 drops out most often.
This is due to the fact that Math.round () receives a variety of random numbers from 1 to 3, but rounding to the nearest integer will result in:
values from the range 1 ... 1.49999 .. become 1 values from the range 1.5 ... 2.49999 .. become 2 values from the range 2.5 ... 2.99999 .. become 3 From here it is clearly seen that the range falls in 1 (as well as 3) values are two times smaller than in 2. Because of this, such a bias.