1
00:00:00,330 --> 00:00:00,960
Welcome back.

2
00:00:00,960 --> 00:00:06,270
In this lesson, we are going to be looking at math, random, so every game you produce, almost every

3
00:00:06,270 --> 00:00:11,850
game you produce is going to need some type of way to produce some type of unexpected result.

4
00:00:11,940 --> 00:00:13,410
And you can do that with math.

5
00:00:13,410 --> 00:00:20,900
Random and JavaScript provides a nice, neat way to return a value, a random value, anywhere from

6
00:00:20,920 --> 00:00:25,520
zero to one, but not inclusive of one.

7
00:00:25,890 --> 00:00:28,500
So it's anywhere from zero to almost one.

8
00:00:28,800 --> 00:00:30,950
And there's an example here on their website.

9
00:00:31,290 --> 00:00:38,610
So if you go down here and hit run, you can see that the math random multiplies by a value and this

10
00:00:38,610 --> 00:00:43,170
is going to return back the number that we're have a possibility of returning.

11
00:00:43,410 --> 00:00:48,720
And the reason that we use math floor is to get rid of basically the decimal with all the different

12
00:00:48,930 --> 00:00:49,930
values to it.

13
00:00:50,130 --> 00:00:57,720
So what math floor does is it allows us to bring the value down at least integer value.

14
00:00:58,050 --> 00:01:02,180
And basically what it's doing is it's just cutting off any of the decimal points.

15
00:01:02,430 --> 00:01:07,890
So you can think of it that way, that even if it's five point nine five, it ends up as five five point

16
00:01:07,890 --> 00:01:09,760
zero five still ends up as five.

17
00:01:10,080 --> 00:01:13,650
Let's go ahead and try this out and bring this into our application.

18
00:01:14,190 --> 00:01:18,980
So using math random and this is another one that we're going to have to reuse.

19
00:01:19,260 --> 00:01:20,940
So just create a function for it.

20
00:01:20,940 --> 00:01:27,990
And we could call it randt, be expecting a numeric value to be passed in there and creating a temporary.

21
00:01:28,290 --> 00:01:34,680
So we're going to use the math, random value, the math, math, random method, and we're going to

22
00:01:34,680 --> 00:01:37,740
multiply it by whatever the value of numbers is.

23
00:01:38,370 --> 00:01:41,460
So when I refresh it here and I'll show you how that's going to work.

24
00:01:41,460 --> 00:01:48,580
So, Rande, and let's put in a number of 400, and we do need to return that to see a value there.

25
00:01:48,600 --> 00:01:49,260
So temp.

26
00:01:49,260 --> 00:01:51,380
So let's return that back.

27
00:01:51,930 --> 00:01:55,400
So try that one more time round and we'll try four hundred again.

28
00:01:55,770 --> 00:01:59,770
We can see that it's getting a random value being returned back.

29
00:01:59,970 --> 00:02:05,040
So this is always going to be a different value and it's going to be anywhere from zero all the way

30
00:02:05,040 --> 00:02:08,910
up to three ninety nine point nine nine nine nine nine and so on.

31
00:02:09,090 --> 00:02:15,630
And in order to clear up all of those decimal places, then this is where we can use math flaw that

32
00:02:15,630 --> 00:02:18,550
will give us the ability to clear up those decimal places.

33
00:02:18,810 --> 00:02:24,480
So in those instances when you don't want to see all the decimal places, you can just add that in and

34
00:02:24,480 --> 00:02:25,680
we can do a quick refresh.

35
00:02:25,830 --> 00:02:29,810
And now you can see that we can type in rent.

36
00:02:30,330 --> 00:02:36,320
So try that random four hundred again and you can see that we are getting just random values to it.

37
00:02:36,660 --> 00:02:42,030
So we're getting without any of the decimal places, so are able to return back these random values.

38
00:02:42,150 --> 00:02:47,520
And this is actually exactly what we want within our gameplay and we're going to be using this random

39
00:02:47,520 --> 00:02:49,660
function several times coming up.

40
00:02:49,920 --> 00:02:51,180
So go ahead and try this out.

41
00:02:51,180 --> 00:02:57,750
Added into your project and get familiar with using the math random and also the math floor to get rid

42
00:02:57,750 --> 00:03:02,520
of the decimal places so that you have a nice clean number that's being returned and if you want to

43
00:03:02,520 --> 00:03:03,480
return it back.

44
00:03:03,510 --> 00:03:09,650
So essentially what's happening here is it's returning back a value from zero to whatever the number

45
00:03:09,670 --> 00:03:12,480
value here is not matching that number of value.

46
00:03:12,630 --> 00:03:17,760
And if we do want to return back one to four hundred, we could always add one in there.

47
00:03:17,760 --> 00:03:18,720
But I'm not going to do that.

48
00:03:18,730 --> 00:03:23,100
I'm just going to leave it as anywhere from zero to three ninety nine.

49
00:03:23,190 --> 00:03:24,610
So it'll never hit 400.

50
00:03:24,720 --> 00:03:30,870
So even if we do math random and if we do two, you're going to see that no matter how many times I

51
00:03:30,870 --> 00:03:32,520
do it, it's not going to hit two.

52
00:03:32,550 --> 00:03:34,470
So it's going to be either a one or a zero.

53
00:03:34,680 --> 00:03:41,070
And that's where if we wanted to have it as a one or a two, then that's why we'd have to add in a one

54
00:03:41,070 --> 00:03:43,370
there, because, again, we are using the math for.

55
00:03:43,680 --> 00:03:45,120
So go ahead and try this out for yourself.
