0:00:01.477,0:00:04.184
Here's a function[br]you might not know about: random.
0:00:04.184,0:00:07.337
It takes in two parameters:[br]a lower bound and an upper bound,
0:00:07.337,0:00:10.587
and it gives you back a random number[br]somewhere between those two bounds.
0:00:10.587,0:00:14.392
So here, this variable number will be[br]somewhere between zero and one.
0:00:14.392,0:00:18.152
Then we're going to draw that number[br]to the canvas using this text function.
0:00:18.152,0:00:21.868
Those last two parameters are[br]for the x and y coordinates of the text
0:00:21.868,0:00:25.558
and we use textSize and fill[br]to set the fill and the color of the text.
0:00:25.558,0:00:27.995
So if I press Restart a couple of times,
0:00:27.995,0:00:30.245
you can see the random numbers[br]being generated,
0:00:30.245,0:00:33.632
and you'll see the precision of[br]these numbers is to three decimal places.
0:00:33.632,0:00:35.105
So here's a question--
0:00:35.105,0:00:38.365
what if I only wanted to generate[br]a number that's either zero or one?
0:00:38.365,0:00:41.793
Well, we can use[br]this other function called round,
0:00:41.793,0:00:45.753
and this takes in a number that can[br]have as many decimals as you want,
0:00:45.753,0:00:47.923
and it rounds it to the nearest integer.
0:00:47.923,0:00:51.828
So I'm just going to go ahead[br]and make a new variable called integer
0:00:51.828,0:00:53.923
and assign it whatever round gives us.
0:00:53.923,0:00:55.973
We can also draw that integer[br]to the screen
0:00:55.973,0:00:58.173
with our handy-dandy text function.
0:00:58.173,0:01:00.385
So text(integer
0:01:00.385,0:01:06.235
--we'll put it maybe at 160[br]and 350. Nice.
0:01:06.235,0:01:12.115
So this shows us that round of 0.2314[br]rounds down to zero,
0:01:12.115,0:01:18.007
and if I put in something like 4.6,[br]that would round me up to 5. Neat.
0:01:18.007,0:01:20.586
So if I wanted to randomly generate[br]a zero or a one,
0:01:20.586,0:01:24.256
I can take this random decimal[br]that we're generating,
0:01:24.256,0:01:27.366
that falls between zero and one,
0:01:28.006,0:01:30.596
and stick it right into[br]that round function.
0:01:30.596,0:01:32.991
So just like this:[br]I'm going to grab this number
0:01:32.991,0:01:35.411
and plop it down here.
0:01:35.411,0:01:37.013
And now you can see
0:01:37.013,0:01:39.543
that whenever we generate[br]a number that's less than 0.5,
0:01:39.543,0:01:41.353
it gets rounded down to zero,
0:01:41.353,0:01:44.777
and whenever we generate a number[br]that is greater than or equal to 0.5,
0:01:44.777,0:01:46.747
it gets rounded up to one.
0:01:47.597,0:01:51.318
Maybe you can start to see the beginning[br]of some sort of coin-flip game here
0:01:51.318,0:01:54.918
where if you flip a zero,[br]your friend gives you a dollar,
0:01:54.918,0:01:57.588
and if you flip a one,[br]your friend gives you ten dollars.
0:01:57.588,0:01:58.734
Great game, right?
0:01:58.734,0:02:01.538
In fact, let's go ahead[br]and illustrate this coin-flipping game
0:02:01.538,0:02:03.328
with some super realistic coins
0:02:03.328,0:02:06.088
that just happen to look[br]like really boring ellipses.
0:02:06.088,0:02:10.604
Just like this: I'm going to draw[br]an ellipse in the middle of our canvas
0:02:10.604,0:02:12.294
and that's going to be our coin.
0:02:12.294,0:02:15.284
Ooh! It's covering the text.[br]Let's scoot that up a bit.
0:02:16.774,0:02:20.709
Great, and I have this idea[br]where, if I flip a zero,
0:02:20.709,0:02:23.249
I'm going to show[br]the purple side of the coin,
0:02:23.249,0:02:28.079
so to make the coin purple,[br]I can just fill it with some purple.
0:02:28.739,0:02:32.948
If I flip a 1,[br]I'll show the yellow side of the coin
0:02:32.948,0:02:35.308
so it'll be a purple[br]and yellow-sided coin.
0:02:35.308,0:02:37.778
And luckily, with our impressive[br]knowledge of if-statements
0:02:37.788,0:02:39.333
this is super easy.
0:02:39.333,0:02:44.571
We can just say if integer equals zero,
0:02:44.571,0:02:48.221
(remember we use[br]three equals signs to check for equality),
0:02:48.221,0:02:53.111
then we will fill the ellipse purple.
0:02:53.541,0:02:57.642
Then, if integer is equal to one,
0:02:58.872,0:03:01.122
we have a different fill function
0:03:02.282,0:03:04.342
and we'll make that one yellow.
0:03:06.302,0:03:09.428
Great. And it works! Woo hoo!
0:03:09.428,0:03:11.408
But let's think about this for a second--
0:03:11.408,0:03:14.248
integer here will only ever[br]be zero or one, right?
0:03:14.248,0:03:15.768
We designed it that way
0:03:15.768,0:03:18.932
so that means[br]that either this statement will be true,
0:03:18.932,0:03:21.200
or this statement will be true
0:03:21.200,0:03:22.294
Always.
0:03:22.294,0:03:24.680
We've covered every possible case here, which means
0:03:24.680,0:03:27.670
we can start thinking about[br]our decision-making a little differently.
0:03:27.670,0:03:32.368
That is, if integer is equal to zero,[br]we will fill it purple,
0:03:33.018,0:03:35.298
otherwise, we'll fill yellow.
0:03:35.928,0:03:38.239
So, do you see[br]how we don't have to say anything
0:03:38.239,0:03:40.589
about integer being one[br]in that second case?
0:03:40.589,0:03:41.805
All we have to say is,
0:03:41.805,0:03:44.875
"If integer is zero, do this;[br]otherwise, do that",
0:03:44.875,0:03:47.983
and, in programming,[br]the way we say "otherwise" is "else."
0:03:47.983,0:03:49.241
So watch this:
0:03:49.241,0:03:53.101
I'm just going to replace this second[br]if-condition with the word "else"
0:03:53.101,0:03:54.622
and what this means is,
0:03:54.622,0:03:57.532
if the stuff inside these parentheses[br]is true,
0:03:57.532,0:03:59.652
then run the code in these brackets.
0:03:59.652,0:04:02.427
Otherwise, run the code in these brackets.
0:04:02.797,0:04:06.260
Sometimes we'll even put the "else"[br]on the same line as that closing bracket
0:04:06.260,0:04:07.720
just to remind ourselves
0:04:07.720,0:04:10.310
that these two blocks of code[br]are very, very connected.
0:04:10.310,0:04:13.801
You can't have an else block[br]unless you've just had an if block.
0:04:13.801,0:04:14.951
Got it?
0:04:15.411,0:04:18.131
This will also help you remember
0:04:18.131,0:04:23.141
to not put something in between[br]the two blocks, like var y equals zero,
0:04:23.141,0:04:26.291
and that would just break everything![br]So don't do that.
0:04:27.031,0:04:29.900
Great. So now we know about if/else[br]which is really good
0:04:29.900,0:04:32.660
when we are deciding[br]between two possible things to do.
0:04:32.660,0:04:34.458
But what if we have more?
0:04:34.458,0:04:36.878
What if I generated[br]an integer between zero and two
0:04:36.878,0:04:40.408
and then I had three possibilities:[br]zero, one, or two? What then?
0:04:40.408,0:04:42.978
Duh duh duh! To be continued!