Instructor: So next up, a one-off video
on something called the Math object.
Built into JavaScript out of the box for free,
we get access to this thing called Math with a capital M
and it's just a collection of properties
and methods that have to do with Math in some way.
So we have methods like rounding or round.
We have a method called abs for absolute value.
We have constants, mathematical constants like pi,
the value of pi 3.14159, and so on,
which at one point,
I knew the first 200 digits of in like fourth, fifth grade.
We had a competition at my school.
Anyway, I've lost almost all of that.
So this Math object is here in the console.
I don't have to do anything.
I can just reference it
and I can actually just hit enter on Math and expand this.
So we haven't talked about objects yet
and you don't need to really understand what an object is
in JavaScript except for the fact
that it's a collection of properties and methods.
So one option would've been
to make just a variable called PI
that we always have access to.
And another one called E through Euler's number
or you know, the square root of two.
That could just be available anywhere, right?
So I could just do that or JavaScript could do that.
It doesn't because instead of just polluting the namespace,
which basically means instead
of just having a bunch of variable names taken up,
JavaScript's approach was to combine all
of those into a single object.
So they're all nested under this thing called Math.
So I have to access Math.PI
or Math.E for Euler's number.
You probably won't really need either of these,
at least not with what we're gonna focus on for now.
But they are there.
But what I wanna focus on are the methods.
These methods range from things that you probably
won't need to use at all, like, I don't know,
arctan or what is it?
Cosine, sine.
Square root you may wanna know about.
But the ones I'll be showing
are mainly around random numbers
and a little bit around rounding
and flooring your numbers.
So you can read more about the Math object on MDN.
There's a page for every single method if you wanted
to learn about sine and how it works.
Not necessarily the Math behind it,
but how the sine method works
and if it expects any arguments
and how they work and all of that.
But I'll just show a couple of these methods
right off the bat.
First of all, we've got one called floor.
Math.floor will take a number with a decimal
and chop off that decimal, so it's not rounding.
This could be 9999999, which would round up to 24.
It's just chopping it off.
So that's floor.
We also have ceiling or ceil.
So I could have 34.1
and it's going to round it up no matter what.
So 34.9999 is going to ceil or ceiling
to three to 35, and 34.01 will also round up
or ceiling up to 35.
So those, eh, maybe not that useful on their own.
But what I'm gonna show you now
is how we generate random numbers.
So there's a built-in method to that Math object
and this is really the whole reason I'm showing you
the Math object in the first place.
There's a built-in method called random.
And random is going to give us a random decimal number
between zero and one, but it won't actually include one.
So it's going to be between zero and .999999999.
Math.random, just like that.
So every time I call that,
it is a method, I need those parentheses.
Every time I call it, I get a different very long decimal.
Usually, well, sometimes that's fine, but most of the time,
if we're trying to get a random number,
we might be trying to get a random number
between one and two.
We might want a coin flip or zero and one
where it's just integers.
The whole number one, whole number two,
like what's your favorite number between one and 100?
I don't wanna hear, you know, 94.12815934.
I just wanna hear 91 or 92 or whatever the number is.
So to do that requires a bit more work.
We don't actually have a method called random integer
or random int or something like that.
Many other programming languages do.
In JavaScript, we don't.
But we can do some Math.
We can take a number like this
and then floor it in order to get a whole number.
So it's a multi-step process.
Let's say we want random numbers between one and 10.
I can call Math.random.
So that gives me a very long decimal between zero and one.
Then I can multiply by 10.
So now I'm looking at 5.961, blah, blah, blah.
Then I can floor that to just get five.
Okay, so let's try that.
Let's do a number between one and five.
So Math.random, and I'm gonna multiply by five.
I want a number between one and five.
So that gives me 3.7 this time.
Next time, it's two point.
Nope, that gave me 1.8, 4.1, 4.2, 0.8.
Okay.
And then I'm gonna Math.floor that
but I wanna floor the entire thing.
So what I did here was put it in a variable
and then floor that, but that's a bit extra.
I don't really need this, I don't need this step on its own.
So I'm gonna do a Math.floor.
Remember what floor does, it chops off the decimal.
So Math.floor of a random number multiplied by five.
Okay, so we're getting 0, 3, 4, 3, 4, 2, 1.
So I am getting five random numbers.
The problem I have now is that it's going from zero, right?
Our random numbers, just Math.random in general goes
from zero to one, non-inclusive, then I multiply it by five.
I'm never actually going to have five as a random number.
I'm gonna have zero up until four.
So if I want one to five,
I then need to shift everything up by adding one.
So this will give me a random number from one to five.
Now, what if I wanted to go from one to 10?
Well, I just multiply this by 10.
Now I have random numbers
between one and 10, random integers.
What if I wanna go from 20 to 22?
Well, all I need to do is figure out how many options,
how many numbers am I trying to generate?
Well, 20, 21 and 22, that's three numbers.
And then I can shift this upwards by 20.
So I get 20, 21, 22.
So not that common to do that.
It's most, I'd say most common to generate a number starting
from either zero to something or one to something.
But if you ever need a range,
you can shift it upwards as much as needed.
So that is Math.random.
On its own, it gives us a random decimal from zero to one.
We can transform that
and use Math.floor to help us get integers
where we don't have that crazy looking decimal.
So we have other methods on the Math object
round absolute value, power,
which will raise something to a power.
Although we already know how to do that,
we don't really need the Math object, right?
We can do this.
Math.power of,
let's say two to the third is the same.
It's just 2 ** 3.
So if you have any other questions about the Math object,
take a look at MDN.
Remember, we have not really covered objects at all.
So all that we know right now is that an object,
it's just a a place to store stuff
so it's not all hanging out on its own
in the middle of nowhere.
All this Math functionality has been combined
into a single object,
a single entity called Math with a capital M.