It says .random(): “This method returns a random number between 0 (inclusive) and 1 (exclusive)”. Does that mean it’s never going to be 1? why?
No, it is never going to be 1. The why is simple, that would give two possible integers, which would need a quantum computer to deal with (just kidding). We cannot have more than 1 integer returned from the method, so the generated value can never equal 1.
Thanks. Why can’t we have more than 1 integer returned?
That is a good question, if none of what I explained earlier made any sense. It would mean we would already have a way to generate both 0 and 1 with the same function. We could hash out the integers and have a boolean generator. But lo, to do that we have to write our own function…
const choice = a => a[Math.floor(Math.random() * a.length + Math.min(...a))] console.log(choice([0, 1])) // 0 or 1 console.log(choice([1,2,3,4,5,6])) // 1 .. 6
If we have to go that far, then we may as well go the distance.
const choice = a => a[Math.floor(Math.random() * a.length + (Math.min(...a) || 0))] console.log(choice(['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']))
// indigo (or one of red .. violet)
The point being made is that we need some level of predictability and as soon as that breaks down, the logic breaks down. With this function (random) we know with certainty that only 1 integer can result, and that is zero. On that certainty we can now build a program.
Zero is included in the solution set. It is not a limit, but a fixed value. 1 is not included in the solution since it is the limit.
lim(x) | x -> 1 // x approaches 1 from the left but never reaches
And it follows we are in the positive domain.