1
00:00:00,150 --> 00:00:06,390
In this lesson, we are going to be looking at not random, so math is a built in JavaScript function

2
00:00:06,570 --> 00:00:09,450
and there's quite a few things that you can do with math.

3
00:00:09,450 --> 00:00:16,470
And as you can see, Mozilla Developer Network lets out a bunch of the different methods that are available,

4
00:00:16,470 --> 00:00:19,400
as well as the properties that are available within that.

5
00:00:19,590 --> 00:00:21,480
And there's quite a lot going on here.

6
00:00:21,690 --> 00:00:24,750
The one that we're going to be making use of is math random.

7
00:00:24,900 --> 00:00:31,610
And what not random does is it gives us a number from zero to one, inclusive of zero, but not one.

8
00:00:31,830 --> 00:00:34,560
So it's going to give us almost one, but not one.

9
00:00:34,800 --> 00:00:39,000
And that's important to remember that you're never going to get to one within math random.

10
00:00:39,000 --> 00:00:44,730
And that's what we use, things like math flaw and so on, which is going to give us some more options

11
00:00:45,120 --> 00:00:50,160
when we are multiplying out those values of math random that are being returned back.

12
00:00:50,430 --> 00:00:52,200
And you can see it being run here.

13
00:00:52,320 --> 00:00:54,750
So you can go over to the modular developer network.

14
00:00:54,750 --> 00:00:57,230
Always a great resource and you can try that out.

15
00:00:57,240 --> 00:00:58,860
They've got a bunch of code examples.

16
00:00:59,130 --> 00:01:01,590
And the one that we're looking at is math random.

17
00:01:01,800 --> 00:01:08,910
And we want to actually turn this math random into a number from anywhere from one to six.

18
00:01:09,120 --> 00:01:10,680
And I'll show you how to do that.

19
00:01:10,800 --> 00:01:17,010
And we can do that by multiplying it by the number that we've got within the role.

20
00:01:17,520 --> 00:01:21,870
So let's go back into our application and make use of math random.

21
00:01:22,140 --> 00:01:27,950
We have our button that gets clicked and this is where the arole starts.

22
00:01:28,560 --> 00:01:31,880
So we're going to make sure and we're going to do a quick roll.

23
00:01:32,610 --> 00:01:37,520
So setting up and we'll get both of those roles within an array.

24
00:01:37,830 --> 00:01:44,130
So we'll have the first person's role and the number that we want to use for the role so we can have

25
00:01:44,460 --> 00:01:46,940
rolling up to six or we could even do more.

26
00:01:47,730 --> 00:01:51,480
And then I'll get a console log out the rules value.

27
00:01:51,510 --> 00:01:55,260
So every time we click the button, we're going to get the value of roles.

28
00:01:55,440 --> 00:02:02,280
And there's one thing missing for those of you that are watching that we need to have a value for role.

29
00:02:02,430 --> 00:02:06,540
It's going to take in a numeric value so it can be.

30
00:02:06,750 --> 00:02:09,390
And in this case, we're rolling anywhere to six.

31
00:02:09,690 --> 00:02:14,760
So we need a random number to return back from one to six.

32
00:02:15,000 --> 00:02:20,640
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

33
00:02:20,640 --> 00:02:24,050
role number and we can use math at random.

34
00:02:24,810 --> 00:02:26,580
So this is going to return back a number.

35
00:02:26,580 --> 00:02:33,150
And as we saw, it's going to return back a number that's zero decimal and a bunch of digits after that.

36
00:02:33,480 --> 00:02:39,030
And that's not what we need because we need to have an actual value that's being returned back that

37
00:02:39,030 --> 00:02:39,940
we can make use of.

38
00:02:40,170 --> 00:02:44,940
So if we multiply it by nine, so we return back to our number first.

39
00:02:45,390 --> 00:02:52,530
So we see that we get two random numbers every time we roll and we're multiplying it by the value that's

40
00:02:52,530 --> 00:02:53,610
being passed in there.

41
00:02:53,790 --> 00:02:55,590
So we've got zero.

42
00:02:55,590 --> 00:02:56,540
We've got one.

43
00:02:56,550 --> 00:02:57,710
Let's try to roll again.

44
00:02:57,720 --> 00:03:02,970
So there's a two and a one point nine and three point nine and so on.

45
00:03:03,180 --> 00:03:04,440
So it doesn't look great.

46
00:03:04,710 --> 00:03:09,160
And the way that we can solve that is by applying another math function.

47
00:03:09,450 --> 00:03:15,720
So going back into our trusted Odzala developer network, we're going to see math flaw at what math

48
00:03:15,720 --> 00:03:16,890
flaw is going to do.

49
00:03:16,890 --> 00:03:23,060
It's going to return to the largest integer less than or equivalent to the given a number.

50
00:03:23,280 --> 00:03:29,730
So what that does when we try that out within the code, if we have five point nine five, it's going

51
00:03:29,730 --> 00:03:30,780
to bring it down to five.

52
00:03:30,930 --> 00:03:34,170
If we have five point zero five, it's still going to be five.

53
00:03:34,320 --> 00:03:37,290
If we have five, it's going to be still five.

54
00:03:37,410 --> 00:03:43,620
And if we have negative six as could be returned back or a negative five point zero five where it's

55
00:03:43,620 --> 00:03:45,830
going to be rounding it to the floor of the opposites.

56
00:03:45,870 --> 00:03:48,840
And so that's where we're going to be coming up with negative six.

57
00:03:49,290 --> 00:03:51,590
So that's essentially how the math works.

58
00:03:51,900 --> 00:03:59,010
So when we apply a math flaw, so we're going to wrap our math random with a math flaw.

59
00:03:59,220 --> 00:04:03,470
And what this is going to really do is going to get rid of all these decimal places.

60
00:04:03,480 --> 00:04:04,260
So let's try that out.

61
00:04:04,860 --> 00:04:11,280
So we see that we've got three to zero, one to two, five to five, three to two.

62
00:04:11,460 --> 00:04:17,940
And you're probably thinking, wait a minute, we need a number from one to six and we've got zero one,

63
00:04:17,940 --> 00:04:18,780
two, three.

64
00:04:19,020 --> 00:04:21,840
And we could probably get four if we kept pressing that rule.

65
00:04:21,960 --> 00:04:24,860
And we've got all the way up to five, but we're not hitting six.

66
00:04:25,200 --> 00:04:27,750
So the solution to that is really simple.

67
00:04:27,750 --> 00:04:29,130
We're just adding one to it.

68
00:04:29,400 --> 00:04:32,310
And now we're going to get a number from one to six.

69
00:04:32,550 --> 00:04:36,870
So I've got two fives, six a two and a one in a three.

70
00:04:37,020 --> 00:04:39,150
And you see that there's no zeros, no sevens.

71
00:04:39,420 --> 00:04:41,280
We've got the number that we were looking for.

72
00:04:41,520 --> 00:04:44,480
So that's how we can create and initiate that role.

73
00:04:44,910 --> 00:04:47,430
So the next thing that we want to do is apply some logic.

74
00:04:47,430 --> 00:04:48,540
We've got the two roles.

75
00:04:48,720 --> 00:04:55,680
We've got the players role and the computer's role, and we need to do a comparison of those role values.

76
00:04:56,010 --> 00:04:58,080
So all of that is coming in the next lesson.

77
00:04:58,080 --> 00:04:59,850
So go ahead and try a math floor.

78
00:05:01,870 --> 00:05:07,150
Azmath is one of those things that almost every application you build, where you've got some type of

79
00:05:07,150 --> 00:05:13,450
game and you want something to happen at random unexpectedly, you're going to need to use math random.
