Why has the author of the quiz written the function like this . Look below

function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } var temp = getRandomInt(0,3); alert(temp);

The part that confuses me is why has he added "+min" at the end , what possibly does he want to achieve ?

Thanks .

gautam

### Hi gautamz07, Math.random()

Hi gautamz07,

Math.random() returns a number greater then or equal to 0 and less then 1.

0 times anything is 0, so adding min to the end just ensures you have a number at least equal to min.

### Thanks

Thank you tony , But i still don't get why the + min at the end ?

### max - min defines the range.

max - min defines the range. Multiply by the result of math.random() and you need to then add the min value.

Step by step for a min value of 4 and max value of 7

random() -> value of 0 =< x < 1, for example, 0.6

times the range 3 -> 1.8

1.8 + min = 5.8 which is => 4 and < 7

cheers,

gary

### Thanks gary

Thanks for your elaborate explanation @gary