The next thing that we need is a random number, so we've got a message to the user and the way that
we usually build out the game is that we look at what's next.
So the user comes in, they get a welcome message.
We provide some instructions for the game.
The input is there, the guess button is there so they know what to do, what actions to take.
And the next action is going to be to generate a random number.
And then we need to create the interaction where we can pick up the input field value and have an event
listener on the guess button and then take that value and check it against the value that we've we've
generated as our number value.
So going into our JavaScript code, we're going to create a function in order to handle this.
And this function will be random number function.
It's going to take into parameters, can have a minimum and a maximum value, and it's going to be returning
back that minimum and maximum value as a random number.
So using math flaw and the way that we usually do this is we either do math flaw or math ceiling.
So that gives us more control over what the number we're expecting.
So math flaw will bring it down to the nearest whole number, math ceiling.
Will it bring it up to the nearest full number?
And so if it's a one point, one math flaw will be one.
That's one point nine.
Math flaw will also be one.
And with math random, what gets returned with this math?
Random function is a random number.
So it starts at zero and it's got decimal.
So it will always be a different value, a different number.
You see, when we changed it, it gives us this big, long number, zero point two to two and so on.
And we enter it again.
We're going to get a different number.
And the way that we use math random is we take this number and we multiply it.
So we'll take math random and we multiply it by 20.
So that's going to give us a whole number minus the decimal, whatever, whatever is going to give us
a random number, times 20.
So this is going to be actually a number from zero decimal whatever, all the way up to 19 decimal whatever.
So that's why we use math floor.
So it gives us more control and we can make sure that with math for this math random number.
And I'll just put in times 20 and show you what this formula returns back to, clearing that out and
doing math floor math twenty.
It's going to give us a random number so it could be any random number.
And we see that it's fluctuating and it's anywhere from zero to 20 because we're using math floor and
math ceiling will convert it to the ceiling.
So that will bring it up.
So we're using math floor in this case.
So this is another part of the formula where we've got our max value subtracting our men value.
So if our max value is ten, our men value is one that's going to give us a number of nine.
And we also need to add plus one.
And when we add the minimum to it, that's going to ensure that we always return back at least the minimum
value within this formula.
So what's going to happen now is whenever we run this function, so we cleared this function out and
we run the function random number, it's expecting two parameters.
So if we want a number from one to five, we can see that we get a random number from one to five.
And these numbers are always going to fall anywhere from one to five.
We're not going to hit zero because we added the minimum and we're not going to go over five because
we subtracted the minimum from the maximum and added one.
So that will always end up from a number from anywhere from one to five.
And now that we've got that in a function, we can utilize it whenever we want and we want to make use
of it when we're initiating the game, because this is where we're starting the game play and we're
creating that number that needs to be guessed.
We can add that into the game.
We can just call it game num.
So this is the number that the player is trying to guess and it's taking in a value, returning back
that random number.
So just as we saw with the formula works, we need to pass in the minimum and the maximum.
And the reason that I'm not just using the game minimum and maximum within the function here, I could
we could just do not pass in any parameters is that this is going to make that function more flexible.
So if we need more randomness somewhere down the line within the game before extending on it, this
gives us the flexibility in order to utilize that same function with the minimum and maximum.
And we can use that in any number of situations.
Whereas if we hardcoded the game minimum and the game maximum, that's going to not provide us any of
that flexibility.
So now we're creating that game value.
So saving this.
And if I go into game, you're going to see that I've got all of the values, all of the variables that
I'm using within the game and that I need within the game in order to produce the gameplay.
So I've got a minimum I've got to make.
So they're all flexible.
I've got the no random number that was generated and I've got the ability to select all of the inputs
that are available to the user.
So go ahead and add in the ability to generate the random number and you're going to be ready to move
on to the next lesson.