math.random() will give us a number between 0 and 1. It could be 0.1, it could be 0.78, whatever. The point is we know it’s between 0-1. In your example, you have a list with 4 things in it:
fruits = [‘apple’,’orange’,’banana’,’strawberry’];
Let’s recall that every item in that list has an index. apple is index 0, orange is index 1, banana index 2 and strawberry is index 3. So! We want a number between 0 and 3 at random, so we get one of the random fruits from the list. Our problem is that
math.random() will only give us between 0-1. That’s where the math comes in I’m afraid. The smallest number
math.random() is going to give us is 0, so we are good on that end. The max is 1. So if we multiply by 4, that 1 turns into 4.
Now you might be wondering, doesn’t that multiplication change any number that comes out of
math.random()? The answer is yes it does. But again we know the smallest is 0 (and zero times anything is still zero), the largest is 1 (and 1 times 4 is 4) and anything in between will fall in that range. It is bound between 0 and 4.
math.random() multiple times:
math.random() = 0.2 —> multiply by 4 —> 0.8
math.random() = 0.7 —> multiply by 4 —> 2.8
math.random() = 0.99 —> multiply by 4 —> 3.96
Like I said, the outcome is bound [0 … 4).
To the question “how did you know to use that”. The simple answer is: practice. The more problems related to math you come across, the easier they will become to understand. You can gain practice directly through coding examples, or you might want to find some algebra and calculus courses online to get better at them