In this lesson, we are going to be looking at not random, so math is a built in JavaScript function
and there's quite a few things that you can do with math.
And as you can see, Mozilla Developer Network lets out a bunch of the different methods that are available,
as well as the properties that are available within that.
And there's quite a lot going on here.
The one that we're going to be making use of is math random.
And what not random does is it gives us a number from zero to one, inclusive of zero, but not one.
So it's going to give us almost one, but not one.
And that's important to remember that you're never going to get to one within math random.
And that's what we use, things like math flaw and so on, which is going to give us some more options
when we are multiplying out those values of math random that are being returned back.
And you can see it being run here.
So you can go over to the modular developer network.
Always a great resource and you can try that out.
They've got a bunch of code examples.
And the one that we're looking at is math random.
And we want to actually turn this math random into a number from anywhere from one to six.
And I'll show you how to do that.
And we can do that by multiplying it by the number that we've got within the role.
So let's go back into our application and make use of math random.
We have our button that gets clicked and this is where the arole starts.
So we're going to make sure and we're going to do a quick roll.
So setting up and we'll get both of those roles within an array.
So we'll have the first person's role and the number that we want to use for the role so we can have
rolling up to six or we could even do more.
And then I'll get a console log out the rules value.
So every time we click the button, we're going to get the value of roles.
And there's one thing missing for those of you that are watching that we need to have a value for role.
It's going to take in a numeric value so it can be.
And in this case, we're rolling anywhere to six.
So we need a random number to return back from one to six.
And the way that we're going to do that is we're going to set up a number so we can do our number four
role number and we can use math at random.
So this is going to return back a number.
And as we saw, it's going to return back a number that's zero decimal and a bunch of digits after that.
And that's not what we need because we need to have an actual value that's being returned back that
we can make use of.
So if we multiply it by nine, so we return back to our number first.
So we see that we get two random numbers every time we roll and we're multiplying it by the value that's
being passed in there.
So we've got zero.
We've got one.
Let's try to roll again.
So there's a two and a one point nine and three point nine and so on.
So it doesn't look great.
And the way that we can solve that is by applying another math function.
So going back into our trusted Odzala developer network, we're going to see math flaw at what math
flaw is going to do.
It's going to return to the largest integer less than or equivalent to the given a number.
So what that does when we try that out within the code, if we have five point nine five, it's going
to bring it down to five.
If we have five point zero five, it's still going to be five.
If we have five, it's going to be still five.
And if we have negative six as could be returned back or a negative five point zero five where it's
going to be rounding it to the floor of the opposites.
And so that's where we're going to be coming up with negative six.
So that's essentially how the math works.
So when we apply a math flaw, so we're going to wrap our math random with a math flaw.
And what this is going to really do is going to get rid of all these decimal places.
So let's try that out.
So we see that we've got three to zero, one to two, five to five, three to two.
And you're probably thinking, wait a minute, we need a number from one to six and we've got zero one,
two, three.
And we could probably get four if we kept pressing that rule.
And we've got all the way up to five, but we're not hitting six.
So the solution to that is really simple.
We're just adding one to it.
And now we're going to get a number from one to six.
So I've got two fives, six a two and a one in a three.
And you see that there's no zeros, no sevens.
We've got the number that we were looking for.
So that's how we can create and initiate that role.
So the next thing that we want to do is apply some logic.
We've got the two roles.
We've got the players role and the computer's role, and we need to do a comparison of those role values.
So all of that is coming in the next lesson.
So go ahead and try a math floor.
Azmath is one of those things that almost every application you build, where you've got some type of
game and you want something to happen at random unexpectedly, you're going to need to use math random.