1
00:00:02,250 --> 00:00:08,250
The next thing that we need is a random number, so we've got a message to the user and the way that

2
00:00:08,280 --> 00:00:12,450
we usually build out the game is that we look at what's next.

3
00:00:12,450 --> 00:00:15,090
So the user comes in, they get a welcome message.

4
00:00:15,270 --> 00:00:17,460
We provide some instructions for the game.

5
00:00:17,460 --> 00:00:22,830
The input is there, the guess button is there so they know what to do, what actions to take.

6
00:00:22,830 --> 00:00:26,010
And the next action is going to be to generate a random number.

7
00:00:26,130 --> 00:00:31,740
And then we need to create the interaction where we can pick up the input field value and have an event

8
00:00:31,740 --> 00:00:37,530
listener on the guess button and then take that value and check it against the value that we've we've

9
00:00:37,530 --> 00:00:39,690
generated as our number value.

10
00:00:40,290 --> 00:00:45,210
So going into our JavaScript code, we're going to create a function in order to handle this.

11
00:00:45,510 --> 00:00:49,170
And this function will be random number function.

12
00:00:49,400 --> 00:00:55,830
It's going to take into parameters, can have a minimum and a maximum value, and it's going to be returning

13
00:00:55,830 --> 00:01:00,350
back that minimum and maximum value as a random number.

14
00:01:00,570 --> 00:01:07,200
So using math flaw and the way that we usually do this is we either do math flaw or math ceiling.

15
00:01:07,200 --> 00:01:10,660
So that gives us more control over what the number we're expecting.

16
00:01:10,680 --> 00:01:14,790
So math flaw will bring it down to the nearest whole number, math ceiling.

17
00:01:14,790 --> 00:01:17,790
Will it bring it up to the nearest full number?

18
00:01:17,940 --> 00:01:22,390
And so if it's a one point, one math flaw will be one.

19
00:01:22,500 --> 00:01:23,640
That's one point nine.

20
00:01:23,640 --> 00:01:25,220
Math flaw will also be one.

21
00:01:25,230 --> 00:01:28,980
And with math random, what gets returned with this math?

22
00:01:28,980 --> 00:01:31,620
Random function is a random number.

23
00:01:31,860 --> 00:01:34,650
So it starts at zero and it's got decimal.

24
00:01:34,810 --> 00:01:38,160
So it will always be a different value, a different number.

25
00:01:38,280 --> 00:01:44,940
You see, when we changed it, it gives us this big, long number, zero point two to two and so on.

26
00:01:44,940 --> 00:01:46,080
And we enter it again.

27
00:01:46,230 --> 00:01:47,640
We're going to get a different number.

28
00:01:47,640 --> 00:01:52,230
And the way that we use math random is we take this number and we multiply it.

29
00:01:52,470 --> 00:01:55,710
So we'll take math random and we multiply it by 20.

30
00:01:55,860 --> 00:02:01,320
So that's going to give us a whole number minus the decimal, whatever, whatever is going to give us

31
00:02:01,320 --> 00:02:03,140
a random number, times 20.

32
00:02:03,300 --> 00:02:10,260
So this is going to be actually a number from zero decimal whatever, all the way up to 19 decimal whatever.

33
00:02:10,290 --> 00:02:11,940
So that's why we use math floor.

34
00:02:11,940 --> 00:02:18,810
So it gives us more control and we can make sure that with math for this math random number.

35
00:02:18,810 --> 00:02:24,210
And I'll just put in times 20 and show you what this formula returns back to, clearing that out and

36
00:02:24,210 --> 00:02:26,370
doing math floor math twenty.

37
00:02:26,580 --> 00:02:31,020
It's going to give us a random number so it could be any random number.

38
00:02:31,050 --> 00:02:37,540
And we see that it's fluctuating and it's anywhere from zero to 20 because we're using math floor and

39
00:02:37,540 --> 00:02:40,710
math ceiling will convert it to the ceiling.

40
00:02:40,710 --> 00:02:41,910
So that will bring it up.

41
00:02:42,150 --> 00:02:44,170
So we're using math floor in this case.

42
00:02:44,190 --> 00:02:50,520
So this is another part of the formula where we've got our max value subtracting our men value.

43
00:02:50,850 --> 00:02:57,930
So if our max value is ten, our men value is one that's going to give us a number of nine.

44
00:02:58,060 --> 00:03:00,330
And we also need to add plus one.

45
00:03:00,330 --> 00:03:08,070
And when we add the minimum to it, that's going to ensure that we always return back at least the minimum

46
00:03:08,070 --> 00:03:09,910
value within this formula.

47
00:03:10,110 --> 00:03:16,380
So what's going to happen now is whenever we run this function, so we cleared this function out and

48
00:03:16,380 --> 00:03:20,570
we run the function random number, it's expecting two parameters.

49
00:03:20,970 --> 00:03:26,930
So if we want a number from one to five, we can see that we get a random number from one to five.

50
00:03:26,940 --> 00:03:30,330
And these numbers are always going to fall anywhere from one to five.

51
00:03:30,510 --> 00:03:35,520
We're not going to hit zero because we added the minimum and we're not going to go over five because

52
00:03:35,520 --> 00:03:39,570
we subtracted the minimum from the maximum and added one.

53
00:03:39,720 --> 00:03:43,950
So that will always end up from a number from anywhere from one to five.

54
00:03:43,960 --> 00:03:48,990
And now that we've got that in a function, we can utilize it whenever we want and we want to make use

55
00:03:48,990 --> 00:03:53,940
of it when we're initiating the game, because this is where we're starting the game play and we're

56
00:03:53,940 --> 00:03:56,490
creating that number that needs to be guessed.

57
00:03:56,760 --> 00:03:58,830
We can add that into the game.

58
00:03:58,830 --> 00:04:00,670
We can just call it game num.

59
00:04:00,690 --> 00:04:06,570
So this is the number that the player is trying to guess and it's taking in a value, returning back

60
00:04:06,570 --> 00:04:07,560
that random number.

61
00:04:07,570 --> 00:04:12,630
So just as we saw with the formula works, we need to pass in the minimum and the maximum.

62
00:04:12,630 --> 00:04:18,510
And the reason that I'm not just using the game minimum and maximum within the function here, I could

63
00:04:18,510 --> 00:04:23,910
we could just do not pass in any parameters is that this is going to make that function more flexible.

64
00:04:24,030 --> 00:04:28,860
So if we need more randomness somewhere down the line within the game before extending on it, this

65
00:04:28,860 --> 00:04:34,230
gives us the flexibility in order to utilize that same function with the minimum and maximum.

66
00:04:34,410 --> 00:04:37,530
And we can use that in any number of situations.

67
00:04:37,710 --> 00:04:43,710
Whereas if we hardcoded the game minimum and the game maximum, that's going to not provide us any of

68
00:04:43,710 --> 00:04:44,660
that flexibility.

69
00:04:44,910 --> 00:04:47,440
So now we're creating that game value.

70
00:04:47,440 --> 00:04:48,450
So saving this.

71
00:04:48,450 --> 00:04:54,390
And if I go into game, you're going to see that I've got all of the values, all of the variables that

72
00:04:54,390 --> 00:04:59,730
I'm using within the game and that I need within the game in order to produce the gameplay.

73
00:04:59,910 --> 00:05:01,760
So I've got a minimum I've got to make.

74
00:05:01,830 --> 00:05:03,240
So they're all flexible.

75
00:05:03,360 --> 00:05:08,970
I've got the no random number that was generated and I've got the ability to select all of the inputs

76
00:05:08,970 --> 00:05:10,090
that are available to the user.

77
00:05:10,110 --> 00:05:14,460
So go ahead and add in the ability to generate the random number and you're going to be ready to move

78
00:05:14,460 --> 00:05:15,390
on to the next lesson.
