Hey and welcome.
This lesson is all about JavaScript math, and we can have a lot of fun with math.
And of course, math is super useful when you're coding JavaScript, especially in generating random
numbers, rounding up, rounding down and rounding numbers in general.
So used quite a bit, especially when you're creating games or any type of interaction.
Random is a great way to go to create some type of unexpected result for the user.
So we've got math floor math sealing.
The difference is that the math floor returns the largest integer less than or equal to a given number.
So what we've got some examples at the Mozilla developer network.
So if you've got five point ninety five five point zero five Barthe four five or Amarth floor negative
five four point zero five.
So what do you think the answer for the first three is?
And if you said five, you are right because they're all rounding down to the nearest value as opposed
to ceiling, which does the opposite and rounds to the returns, the smallest integer greater than or
equal to give a number.
So what it's doing is that it's rounding it up.
So ceiling dot, negative dot 95 is one because it's rounding it up to one.
Ceiling four is going to be four because that's the closest that's the rounded value up ceiling, seven
point zero zero four or zero zero zero zero four.
So anything above seven is going around to eight and opposite.
If we're doing negatives, you see that it returns to the negative number.
So it's not rounding it up as per say, it's rounding it back down to the nearest numbers are going
down.
That's the difference between the negatives there.
And when you run it, you can see that we get the results one four, eight and negative seven.
And then my favorite Amarth random use it all the time is gives you the ability to generate a random
value floating point, pseudo random number between zero and one.
So what that means is when you do math random, you get a random value, zero dot zero Derat and a bunch
of digits after that.
So that's what this is doing, zero to one.
And also keep in mind as well, inclusive of zero, but not one.
So that's why when we are trying to get math random, we use math flaw in order to find two and what's
being returned back.
So random integer.
So the maximum value that we want to receive and we're using my floor to round it down as well as math
floor for the maximum value.
And then we're multiplying that by math random.
So what happens here is in this case, when we use this function, we get value or passing in three
and the math floor is going to be three as well.
And we're multiplying it by math, random, and then we're multiplying it by floor again.
So that way we're getting a value of zero one or two.
And that's why three is never included, because we're not not one when we're rounding it.
Also, if you want to round it up.
So if you want to get one, two or three, in this case, you could use math ceiling.
But most commonly people use math floor and round it down.
And if you want it to have a number of one, two or three, you would just add one to it.
So you see that as well within a lot of expressions.
And then we just have the basic math, random output.
So number between zero and one.
So we notice all of the decimal places.
So they're all zero decimal and a bunch of digits after that for math random.
And of course, there is a challenge at the end of this lesson, and that is to create a function that
can generate a random number.
So getting to inputs where we've got a minimum and a maximum value and then returning back a random
value depending on what is input into the function.
So maximum is exclusive and minimum is inclusive.
So keep that in mind.
Generate this function and I'll walk you through it so you can pause the video here and I'll walk you
through the solution.
Let's open up the ED and we're going to just create our function.
You can call this get random.
It's going to be expecting two parameters.
So minimum and a maximum value to be passed in for the minimum value.
So we're going to take whatever is passed in using math ceiling.
We're going to take that minimum number and we're going to use math ceiling in order to generate the
minimum number and then maximum number.
If we're going to do the same thing, we're going to apply my floor to that maximum number and then
returning a back math floor math random and then multiplying it by maximum, subtracting the minimum
and then adding the minimum amount back into the formula.
So refresh we can in values now into that function where we've got a minimum return value, a maximum
of four, as you can see that now it's generating a random number all the time different.
And the minimum that it's going to be is one.
And the maximum that we're going to return back is going to be three.
So it's never going to hit at four.
And you can use this formula in a number of ways.
So if you want to get a random value from one to five, you can see that now it's returning back for
if you update that and bring it down to two.
So you're always going to get a random value anywhere from one to two, not including the two, because
remember, that was part of it's exclusive of the maximum value.
So it's always returning back one in this case.
So go ahead and try the.