en.javascript.info/1-js/4-data-structures/2-number/9-random-int-min-max/solution.md
2016-03-06 00:59:16 +03:00

66 lines
1.9 KiB
Markdown

# The simple but wrong solution
The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
```js run
function randomInteger(min, max) {
let rnd = min + Math.random() * (max - min);
return Math.round(rnd);
}
alert( randomInteger(1, 3) );
```
The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
If you run the example above many times, you would easily see that `2` appears the most often.
That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
```js no-beautify
values from 1 ... to 1.4999999999 become 1
values from 1.5 ... to 2.4999999999 become 2
values from 2.5 ... to 2.9999999999 become 3
```
Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
# The correct solution
There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 2.5`, thus adding the required probabilities to the edges:
```js run
*!*
function randomInteger(min, max) {
// now rnd is from (min-0.5) to (max+0.5)
let rnd = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rnd);
}
*/!*
alert( randomInteger(1, 3) );
```
An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
```js run
*!*
function randomInteger(min, max) {
// here rnd is from min to (max+1)
let rnd = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
*/!*
alert( randomInteger(1, 3) );
```
Now all intervals are mapped this way:
```js no-beautify
values from 1 ... to 1.9999999999 become 1
values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
```
All intervals have the same length, making the final distribution uniform.