Transcript from the "Random Numbers" Lesson
>> Brian Holt: There's one called math.random, that just returns to a random number between zero and one. So if I call it a bunch of times, it's gonna be a bunch of things like that. But what happens if I wanna make it so something has a 50/50 chance of appearing, right?
[00:00:16] The way I could do that is math.random is greater than .5 right? And then I'm gonna get sometimes true, sometimes false, right? Or if I wanna make it only a 10% chance, then it would be greater than .9. So most of time this is gonna be false. But eventually, one in 10 times it's gonna be true, right?
So you can do things like that to get random numbers.
>> Brian Holt: So you have 10 holes out there, right? And each one needs to have some sort of random schedule to it. So for me, I have them wait at least two seconds before showing up again, right? Cause they go back into their hole you don't want to immediately show back up, that doesn't make any sense.
But you don't want them to wait forever, so I have them wait about a max of 20 seconds. And again the way to do that, that I find useful, so you have math dot random.
>> Brian Holt: Okay? What if I want to turn that into a number randomly between two and 20?
[00:01:19] So I can do math.round or math.floor. Let's just do math.floor since I think it's a little easier to work with. That always rounds down. Okay? Then I'm gonna do math.random times 20, and that's gonna give me a random number between zero and 20. Let's say, you can see this one was zero right there, right?
[00:01:46] So if I just add two, after it's done it's random thing, then it's gonna always be at least two. So I guess that's between two and 22. If you want it to be two and 20, you would do 18, right? So there you go. That math.floor, math.random 18 times plus 2.
[00:02:08] Does that make sense why that's a random number between two and 20? Let's maybe make this a little bit more succinct. So if i say X, a random number equals math.random. Okay, so now X is gonna be assigned that .five, right? And then we'll do X times 18.
[00:02:36] Or random number, sorry random number. Random number times 18. This is gonna give me a number that has a really long decimal point, and I don't care about the decimals, I just care about the whole numbers. So that's when I'm gonna come in and say math.floor randomnumber times 18, and that's when I'm gonna get nine.
[00:03:05] Is that a little bit more, make a bit more sense? Basically what math.random is gonna give you is a percentage, right? So give me a random percentage of 18. Because that's how percentages work like it's 0.021, right? And so if I ask for 50% of 18 then I'm gonna get 19, or 75%, or something like that.