Here's a function you might not know about: random.
It takes in two parameters: a lower bound and an upper bound,
and it gives you back a random number somewhere between those two bounds.
So here, this variable number will be somewhere between zero and one.
Then we're going to draw that number to the canvas using this text function.
Those last two parameters are for the x and y coordinates of the text
and we use textSize and fill to set the fill and the color of the text.
So if I press Restart a couple of times,
you can see the random numbers being generated,
and you'll see the precision of these numbers is to three decimal places.
So here's a question--
what if I only wanted to generate a number that's either zero or one?
Well, we can use this other function called round,
and this takes in a number that can have as many decimals as you want,
and it rounds it to the nearest integer.
So I'm just going to go ahead and make a new variable called integer
and assign it whatever round gives us.
We can also draw that integer to the screen
with our handy-dandy text function.
So text(integer
--we'll put it maybe at 160 and 350. Nice.
So this shows us that round of 0.2314 rounds down to zero,
and if I put in something like 4.6, that would round me up to 5. Neat.
So if I wanted to randomly generate a zero or a one,
I can take this random decimal that we're generating,
that falls between zero and one,
and stick it right into that round function.
So just like this: I'm going to grab this number
and plop it down here.
And now you can see
that whenever we generate a number that's less than 0.5,
it gets rounded down to zero,
and whenever we generate a number that is greater than or equal to 0.5,
it gets rounded up to one.
Maybe you can start to see the beginning of some sort of coin-flip game here
where if you flip a zero, your friend gives you a dollar,
and if you flip a one, your friend gives you ten dollars.
Great game, right?
In fact, let's go ahead and illustrate this coin-flipping game
with some super realistic coins
that just happen to look like really boring ellipses.
Just like this: I'm going to draw an ellipse in the middle of our canvas
and that's going to be our coin.
Ooh! It's covering the text. Let's scoot that up a bit.
Great, and I have this idea where, if I flip a zero,
I'm going to show the purple side of the coin,
so to make the coin purple, I can just fill it with some purple.
If I flip a 1, I'll show the yellow side of the coin
so it'll be a purple and yellow-sided coin.
And luckily, with our impressive knowledge of if-statements
this is super easy.
We can just say if integer equals zero,
(remember we use three equals signs to check for equality),
then we will fill the ellipse purple.
Then, if integer is equal to one,
we have a different fill function
and we'll make that one yellow.
Great. And it works! Woo hoo!
But let's think about this for a second--
integer here will only ever be zero or one, right?
We designed it that way
so that means that either this statement will be true,
or this statement will be true
Always.
We've covered every possible case here, which means
we can start thinking about our decision-making a little differently.
That is, if integer is equal to zero, we will fill it purple,
otherwise, we'll fill yellow.
So, do you see how we don't have to say anything
about integer being one in that second case?
All we have to say is,
"If integer is zero, do this; otherwise, do that",
and, in programming, the way we say "otherwise" is "else."
So watch this:
I'm just going to replace this second if-condition with the word "else"
and what this means is,
if the stuff inside these parentheses is true,
then run the code in these brackets.
Otherwise, run the code in these brackets.
Sometimes we'll even put the "else" on the same line as that closing bracket
just to remind ourselves
that these two blocks of code are very, very connected.
You can't have an else block unless you've just had an if block.
Got it?
This will also help you remember
to not put something in between the two blocks, like var y equals zero,
and that would just break everything! So don't do that.
Great. So now we know about if/else which is really good
when we are deciding between two possible things to do.
But what if we have more?
What if I generated an integer between zero and two
and then I had three possibilities: zero, one, or two? What then?
Duh duh duh! To be continued!