randomInt(min, max) - min value never gets generated in results

This issue has been tracked since 2022-02-01.

Hi Jonas!

On this lesson you're trying to generate a random number between min and max that includes both these values. However, the min value never gets generated with the code you have:

const randomInt = (min, max) =>
Math.floor(Math.random() * (max - min) + 1) + min;
// 0...1 -> 0...(max - min) -> min...max
// console.log(randomInt(10, 20));

For results to include both min and max value you should replace:
Math.floor(Math.random() * (max - min) + 1) + min;

with:
Math.floor(Math.random() * (max + 1 - min) + min);

The issue seems to be that for randomInt(10, 20) you're multiplying the random number by 10 (max - min) and then always adding 1 to it, ensuring that the lowest possible outcome will always be at least 1 above the min value and that the possible outcomes range from 11 to 20 (10 values).
Instead you should be multiplying the random number by 11 (max + 1 - min) so that the range of possible outcomes ranges from 10 to 20 (11 values) and thus includes both min and max values.

Hope this helps and thanks for creating this course. I'm really enjoying it!

More Details About Repo
Owner Name jonasschmedtmann
Repo Name complete-javascript-course
Full Name jonasschmedtmann/complete-javascript-course
Language JavaScript
Created Date 2018-06-14
Updated Date 2023-03-29
Star Count 12137
Watcher Count 850
Fork Count 14403
Issue Count 153

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date