1 00:00:01,477 --> 00:00:04,184 Here's a function you might not know about: random. 2 00:00:04,184 --> 00:00:07,337 It takes in two parameters: a lower bound and an upper bound, 3 00:00:07,337 --> 00:00:10,587 and it gives you back a random number somewhere between those two bounds. 4 00:00:10,587 --> 00:00:14,392 So here, this variable number will be somewhere between zero and one. 5 00:00:14,392 --> 00:00:18,152 Then we're going to draw that number to the canvas using this text function. 6 00:00:18,152 --> 00:00:21,868 Those last two parameters are for the x and y coordinates of the text 7 00:00:21,868 --> 00:00:25,558 and we use textSize and fill to set the fill and the color of the text. 8 00:00:25,558 --> 00:00:27,995 So if I press Restart a couple of times, 9 00:00:27,995 --> 00:00:30,245 you can see the random numbers being generated, 10 00:00:30,245 --> 00:00:33,632 and you'll see the precision of these numbers is to three decimal places. 11 00:00:33,632 --> 00:00:35,105 So here's a question-- 12 00:00:35,105 --> 00:00:38,365 what if I only wanted to generate a number that's either zero or one? 13 00:00:38,365 --> 00:00:41,793 Well, we can use this other function called round, 14 00:00:41,793 --> 00:00:45,753 and this takes in a number that can have as many decimals as you want, 15 00:00:45,753 --> 00:00:47,923 and it rounds it to the nearest integer. 16 00:00:47,923 --> 00:00:51,828 So I'm just going to go ahead and make a new variable called integer 17 00:00:51,828 --> 00:00:53,923 and assign it whatever round gives us. 18 00:00:53,923 --> 00:00:55,973 We can also draw that integer to the screen 19 00:00:55,973 --> 00:00:58,173 with our handy-dandy text function. 20 00:00:58,173 --> 00:01:00,385 So text(integer 21 00:01:00,385 --> 00:01:06,235 --we'll put it maybe at 160 and 350. Nice. 22 00:01:06,235 --> 00:01:12,115 So this shows us that round of 0.2314 rounds down to zero, 23 00:01:12,115 --> 00:01:18,007 and if I put in something like 4.6, that would round me up to 5. Neat. 24 00:01:18,007 --> 00:01:20,586 So if I wanted to randomly generate a zero or a one, 25 00:01:20,586 --> 00:01:24,256 I can take this random decimal that we're generating, 26 00:01:24,256 --> 00:01:27,366 that falls between zero and one, 27 00:01:28,006 --> 00:01:30,596 and stick it right into that round function. 28 00:01:30,596 --> 00:01:32,991 So just like this: I'm going to grab this number 29 00:01:32,991 --> 00:01:35,411 and plop it down here. 30 00:01:35,411 --> 00:01:37,013 And now you can see 31 00:01:37,013 --> 00:01:39,543 that whenever we generate a number that's less than 0.5, 32 00:01:39,543 --> 00:01:41,353 it gets rounded down to zero, 33 00:01:41,353 --> 00:01:44,777 and whenever we generate a number that is greater than or equal to 0.5, 34 00:01:44,777 --> 00:01:46,747 it gets rounded up to one. 35 00:01:47,597 --> 00:01:51,318 Maybe you can start to see the beginning of some sort of coin-flip game here 36 00:01:51,318 --> 00:01:54,918 where if you flip a zero, your friend gives you a dollar, 37 00:01:54,918 --> 00:01:57,588 and if you flip a one, your friend gives you ten dollars. 38 00:01:57,588 --> 00:01:58,734 Great game, right? 39 00:01:58,734 --> 00:02:01,538 In fact, let's go ahead and illustrate this coin-flipping game 40 00:02:01,538 --> 00:02:03,328 with some super realistic coins 41 00:02:03,328 --> 00:02:06,088 that just happen to look like really boring ellipses. 42 00:02:06,088 --> 00:02:10,604 Just like this: I'm going to draw an ellipse in the middle of our canvas 43 00:02:10,604 --> 00:02:12,294 and that's going to be our coin. 44 00:02:12,294 --> 00:02:15,284 Ooh! It's covering the text. Let's scoot that up a bit. 45 00:02:16,774 --> 00:02:20,709 Great, and I have this idea where, if I flip a zero, 46 00:02:20,709 --> 00:02:23,249 I'm going to show the purple side of the coin, 47 00:02:23,249 --> 00:02:28,079 so to make the coin purple, I can just fill it with some purple. 48 00:02:28,739 --> 00:02:32,948 If I flip a 1, I'll show the yellow side of the coin 49 00:02:32,948 --> 00:02:35,308 so it'll be a purple and yellow-sided coin. 50 00:02:35,308 --> 00:02:37,778 And luckily, with our impressive knowledge of if-statements 51 00:02:37,788 --> 00:02:39,333 this is super easy. 52 00:02:39,333 --> 00:02:44,571 We can just say if integer equals zero, 53 00:02:44,571 --> 00:02:48,221 (remember we use three equals signs to check for equality), 54 00:02:48,221 --> 00:02:53,111 then we will fill the ellipse purple. 55 00:02:53,541 --> 00:02:57,642 Then, if integer is equal to one, 56 00:02:58,872 --> 00:03:01,122 we have a different fill function 57 00:03:02,282 --> 00:03:04,342 and we'll make that one yellow. 58 00:03:06,302 --> 00:03:09,428 Great. And it works! Woo hoo! 59 00:03:09,428 --> 00:03:11,408 But let's think about this for a second-- 60 00:03:11,408 --> 00:03:14,248 integer here will only ever be zero or one, right? 61 00:03:14,248 --> 00:03:15,768 We designed it that way 62 00:03:15,768 --> 00:03:18,932 so that means that either this statement will be true, 63 00:03:18,932 --> 00:03:21,200 or this statement will be true 64 00:03:21,200 --> 00:03:22,294 Always. 65 00:03:22,294 --> 00:03:24,680 We've covered every possible case here, which means 66 00:03:24,680 --> 00:03:27,670 we can start thinking about our decision-making a little differently. 67 00:03:27,670 --> 00:03:32,368 That is, if integer is equal to zero, we will fill it purple, 68 00:03:33,018 --> 00:03:35,298 otherwise, we'll fill yellow. 69 00:03:35,928 --> 00:03:38,239 So, do you see how we don't have to say anything 70 00:03:38,239 --> 00:03:40,589 about integer being one in that second case? 71 00:03:40,589 --> 00:03:41,805 All we have to say is, 72 00:03:41,805 --> 00:03:44,875 "If integer is zero, do this; otherwise, do that", 73 00:03:44,875 --> 00:03:47,983 and, in programming, the way we say "otherwise" is "else." 74 00:03:47,983 --> 00:03:49,241 So watch this: 75 00:03:49,241 --> 00:03:53,101 I'm just going to replace this second if-condition with the word "else" 76 00:03:53,101 --> 00:03:54,622 and what this means is, 77 00:03:54,622 --> 00:03:57,532 if the stuff inside these parentheses is true, 78 00:03:57,532 --> 00:03:59,652 then run the code in these brackets. 79 00:03:59,652 --> 00:04:02,427 Otherwise, run the code in these brackets. 80 00:04:02,797 --> 00:04:06,260 Sometimes we'll even put the "else" on the same line as that closing bracket 81 00:04:06,260 --> 00:04:07,720 just to remind ourselves 82 00:04:07,720 --> 00:04:10,310 that these two blocks of code are very, very connected. 83 00:04:10,310 --> 00:04:13,801 You can't have an else block unless you've just had an if block. 84 00:04:13,801 --> 00:04:14,951 Got it? 85 00:04:15,411 --> 00:04:18,131 This will also help you remember 86 00:04:18,131 --> 00:04:23,141 to not put something in between the two blocks, like var y equals zero, 87 00:04:23,141 --> 00:04:26,291 and that would just break everything! So don't do that. 88 00:04:27,031 --> 00:04:29,900 Great. So now we know about if/else which is really good 89 00:04:29,900 --> 00:04:32,660 when we are deciding between two possible things to do. 90 00:04:32,660 --> 00:04:34,458 But what if we have more? 91 00:04:34,458 --> 00:04:36,878 What if I generated an integer between zero and two 92 00:04:36,878 --> 00:04:40,408 and then I had three possibilities: zero, one, or two? What then? 93 00:04:40,408 --> 00:04:42,978 Duh duh duh! To be continued!