1
00:00:00,210 --> 00:00:01,410
Hey and welcome.

2
00:00:01,440 --> 00:00:05,670
This lesson is all about JavaScript math, and we can have a lot of fun with math.

3
00:00:05,670 --> 00:00:11,250
And of course, math is super useful when you're coding JavaScript, especially in generating random

4
00:00:11,250 --> 00:00:15,210
numbers, rounding up, rounding down and rounding numbers in general.

5
00:00:15,420 --> 00:00:19,710
So used quite a bit, especially when you're creating games or any type of interaction.

6
00:00:19,830 --> 00:00:25,270
Random is a great way to go to create some type of unexpected result for the user.

7
00:00:25,590 --> 00:00:28,080
So we've got math floor math sealing.

8
00:00:28,440 --> 00:00:34,800
The difference is that the math floor returns the largest integer less than or equal to a given number.

9
00:00:35,070 --> 00:00:38,880
So what we've got some examples at the Mozilla developer network.

10
00:00:39,120 --> 00:00:47,340
So if you've got five point ninety five five point zero five Barthe four five or Amarth floor negative

11
00:00:47,340 --> 00:00:49,080
five four point zero five.

12
00:00:49,290 --> 00:00:52,140
So what do you think the answer for the first three is?

13
00:00:52,140 --> 00:00:58,530
And if you said five, you are right because they're all rounding down to the nearest value as opposed

14
00:00:58,530 --> 00:01:06,600
to ceiling, which does the opposite and rounds to the returns, the smallest integer greater than or

15
00:01:06,600 --> 00:01:08,030
equal to give a number.

16
00:01:08,280 --> 00:01:10,800
So what it's doing is that it's rounding it up.

17
00:01:11,340 --> 00:01:17,460
So ceiling dot, negative dot 95 is one because it's rounding it up to one.

18
00:01:18,120 --> 00:01:25,920
Ceiling four is going to be four because that's the closest that's the rounded value up ceiling, seven

19
00:01:25,920 --> 00:01:29,490
point zero zero four or zero zero zero zero four.

20
00:01:29,610 --> 00:01:34,440
So anything above seven is going around to eight and opposite.

21
00:01:34,440 --> 00:01:37,920
If we're doing negatives, you see that it returns to the negative number.

22
00:01:38,100 --> 00:01:44,100
So it's not rounding it up as per say, it's rounding it back down to the nearest numbers are going

23
00:01:44,100 --> 00:01:44,360
down.

24
00:01:45,320 --> 00:01:47,220
That's the difference between the negatives there.

25
00:01:47,400 --> 00:01:51,960
And when you run it, you can see that we get the results one four, eight and negative seven.

26
00:01:52,380 --> 00:01:59,100
And then my favorite Amarth random use it all the time is gives you the ability to generate a random

27
00:01:59,100 --> 00:02:03,630
value floating point, pseudo random number between zero and one.

28
00:02:04,290 --> 00:02:12,090
So what that means is when you do math random, you get a random value, zero dot zero Derat and a bunch

29
00:02:12,090 --> 00:02:13,230
of digits after that.

30
00:02:13,230 --> 00:02:16,220
So that's what this is doing, zero to one.

31
00:02:17,160 --> 00:02:21,360
And also keep in mind as well, inclusive of zero, but not one.

32
00:02:21,570 --> 00:02:29,310
So that's why when we are trying to get math random, we use math flaw in order to find two and what's

33
00:02:29,310 --> 00:02:30,420
being returned back.

34
00:02:30,600 --> 00:02:31,800
So random integer.

35
00:02:31,800 --> 00:02:38,520
So the maximum value that we want to receive and we're using my floor to round it down as well as math

36
00:02:38,520 --> 00:02:40,130
floor for the maximum value.

37
00:02:40,140 --> 00:02:42,800
And then we're multiplying that by math random.

38
00:02:43,080 --> 00:02:50,070
So what happens here is in this case, when we use this function, we get value or passing in three

39
00:02:50,250 --> 00:02:54,480
and the math floor is going to be three as well.

40
00:02:54,660 --> 00:02:59,490
And we're multiplying it by math, random, and then we're multiplying it by floor again.

41
00:02:59,680 --> 00:03:04,260
So that way we're getting a value of zero one or two.

42
00:03:04,270 --> 00:03:09,620
And that's why three is never included, because we're not not one when we're rounding it.

43
00:03:10,290 --> 00:03:12,360
Also, if you want to round it up.

44
00:03:12,360 --> 00:03:16,890
So if you want to get one, two or three, in this case, you could use math ceiling.

45
00:03:16,920 --> 00:03:19,950
But most commonly people use math floor and round it down.

46
00:03:21,190 --> 00:03:25,860
And if you want it to have a number of one, two or three, you would just add one to it.

47
00:03:25,870 --> 00:03:28,060
So you see that as well within a lot of expressions.

48
00:03:28,510 --> 00:03:31,600
And then we just have the basic math, random output.

49
00:03:31,750 --> 00:03:33,940
So number between zero and one.

50
00:03:34,120 --> 00:03:35,770
So we notice all of the decimal places.

51
00:03:35,770 --> 00:03:40,770
So they're all zero decimal and a bunch of digits after that for math random.

52
00:03:41,170 --> 00:03:46,900
And of course, there is a challenge at the end of this lesson, and that is to create a function that

53
00:03:46,900 --> 00:03:48,380
can generate a random number.

54
00:03:48,430 --> 00:03:54,790
So getting to inputs where we've got a minimum and a maximum value and then returning back a random

55
00:03:54,790 --> 00:03:57,920
value depending on what is input into the function.

56
00:03:58,120 --> 00:04:02,070
So maximum is exclusive and minimum is inclusive.

57
00:04:02,290 --> 00:04:03,370
So keep that in mind.

58
00:04:03,370 --> 00:04:07,780
Generate this function and I'll walk you through it so you can pause the video here and I'll walk you

59
00:04:07,780 --> 00:04:08,470
through the solution.

60
00:04:08,740 --> 00:04:11,950
Let's open up the ED and we're going to just create our function.

61
00:04:12,420 --> 00:04:14,220
You can call this get random.

62
00:04:14,230 --> 00:04:16,050
It's going to be expecting two parameters.

63
00:04:16,060 --> 00:04:20,820
So minimum and a maximum value to be passed in for the minimum value.

64
00:04:20,840 --> 00:04:25,180
So we're going to take whatever is passed in using math ceiling.

65
00:04:25,600 --> 00:04:30,490
We're going to take that minimum number and we're going to use math ceiling in order to generate the

66
00:04:30,490 --> 00:04:33,460
minimum number and then maximum number.

67
00:04:33,490 --> 00:04:39,820
If we're going to do the same thing, we're going to apply my floor to that maximum number and then

68
00:04:39,820 --> 00:04:48,490
returning a back math floor math random and then multiplying it by maximum, subtracting the minimum

69
00:04:48,850 --> 00:04:52,550
and then adding the minimum amount back into the formula.

70
00:04:52,570 --> 00:05:01,240
So refresh we can in values now into that function where we've got a minimum return value, a maximum

71
00:05:01,240 --> 00:05:06,160
of four, as you can see that now it's generating a random number all the time different.

72
00:05:06,340 --> 00:05:09,280
And the minimum that it's going to be is one.

73
00:05:09,460 --> 00:05:13,350
And the maximum that we're going to return back is going to be three.

74
00:05:13,900 --> 00:05:15,420
So it's never going to hit at four.

75
00:05:15,470 --> 00:05:18,180
And you can use this formula in a number of ways.

76
00:05:18,190 --> 00:05:24,160
So if you want to get a random value from one to five, you can see that now it's returning back for

77
00:05:24,370 --> 00:05:27,040
if you update that and bring it down to two.

78
00:05:27,340 --> 00:05:33,220
So you're always going to get a random value anywhere from one to two, not including the two, because

79
00:05:33,220 --> 00:05:37,370
remember, that was part of it's exclusive of the maximum value.

80
00:05:37,870 --> 00:05:40,230
So it's always returning back one in this case.

81
00:05:40,660 --> 00:05:41,410
So go ahead and try the.
