Welcome back.
In this lesson, we are going to be looking at math, random, so every game you produce, almost every
game you produce is going to need some type of way to produce some type of unexpected result.
And you can do that with math.
Random and JavaScript provides a nice, neat way to return a value, a random value, anywhere from
zero to one, but not inclusive of one.
So it's anywhere from zero to almost one.
And there's an example here on their website.
So if you go down here and hit run, you can see that the math random multiplies by a value and this
is going to return back the number that we're have a possibility of returning.
And the reason that we use math floor is to get rid of basically the decimal with all the different
values to it.
So what math floor does is it allows us to bring the value down at least integer value.
And basically what it's doing is it's just cutting off any of the decimal points.
So you can think of it that way, that even if it's five point nine five, it ends up as five five point
zero five still ends up as five.
Let's go ahead and try this out and bring this into our application.
So using math random and this is another one that we're going to have to reuse.
So just create a function for it.
And we could call it randt, be expecting a numeric value to be passed in there and creating a temporary.
So we're going to use the math, random value, the math, math, random method, and we're going to
multiply it by whatever the value of numbers is.
So when I refresh it here and I'll show you how that's going to work.
So, Rande, and let's put in a number of 400, and we do need to return that to see a value there.
So temp.
So let's return that back.
So try that one more time round and we'll try four hundred again.
We can see that it's getting a random value being returned back.
So this is always going to be a different value and it's going to be anywhere from zero all the way
up to three ninety nine point nine nine nine nine nine and so on.
And in order to clear up all of those decimal places, then this is where we can use math flaw that
will give us the ability to clear up those decimal places.
So in those instances when you don't want to see all the decimal places, you can just add that in and
we can do a quick refresh.
And now you can see that we can type in rent.
So try that random four hundred again and you can see that we are getting just random values to it.
So we're getting without any of the decimal places, so are able to return back these random values.
And this is actually exactly what we want within our gameplay and we're going to be using this random
function several times coming up.
So go ahead and try this out.
Added into your project and get familiar with using the math random and also the math floor to get rid
of the decimal places so that you have a nice clean number that's being returned and if you want to
return it back.
So essentially what's happening here is it's returning back a value from zero to whatever the number
value here is not matching that number of value.
And if we do want to return back one to four hundred, we could always add one in there.
But I'm not going to do that.
I'm just going to leave it as anywhere from zero to three ninety nine.
So it'll never hit 400.
So even if we do math random and if we do two, you're going to see that no matter how many times I
do it, it's not going to hit two.
So it's going to be either a one or a zero.
And that's where if we wanted to have it as a one or a two, then that's why we'd have to add in a one
there, because, again, we are using the math for.
So go ahead and try this out for yourself.