1
00:00:00,790 --> 00:00:06,610
Almost every game you play is going to have random within that game to make it more interesting because

2
00:00:06,610 --> 00:00:10,690
of course, if you have the game, it's really predictable.

3
00:00:10,690 --> 00:00:11,990
It's not going to be that much fun.

4
00:00:12,400 --> 00:00:14,470
And this is where we introduce random.

5
00:00:14,620 --> 00:00:19,420
And with JavaScript, we do have the ability to use math random.

6
00:00:19,600 --> 00:00:29,350
And what that does is that's going to return back a random value anywhere from zero to one within your

7
00:00:29,380 --> 00:00:30,160
JavaScript.

8
00:00:30,590 --> 00:00:32,320
So we'll create a brand new variable.

9
00:00:32,350 --> 00:00:38,470
We can call it computer selection, and we're going to use the built in Amarth random function within

10
00:00:38,470 --> 00:00:39,190
JavaScript.

11
00:00:39,430 --> 00:00:46,180
And I'm going to output this value into the console so that we can see that it's always got a random

12
00:00:46,180 --> 00:00:46,600
value.

13
00:00:46,610 --> 00:00:52,000
So whenever we click one of these buttons, you can see we've got this random value and this value,

14
00:00:52,000 --> 00:00:55,270
as mentioned, anywhere from zero to one.

15
00:00:55,510 --> 00:01:03,520
So we know that 33 percent chance of selecting rock, 33 for paper and 33 for scissors.

16
00:01:03,550 --> 00:01:08,740
So using this formula, we can determine what the computer is going to select.

17
00:01:08,890 --> 00:01:15,550
So taking that value of computer selection, we're going to check to see if it's less than zero point

18
00:01:15,550 --> 00:01:16,230
three four.

19
00:01:16,450 --> 00:01:21,420
So it's roughly around a third chance of the computer selecting that value.

20
00:01:22,240 --> 00:01:29,350
And if it does, then we can set up a new variable or we can just use computer selection and equal that

21
00:01:29,350 --> 00:01:30,280
to rock.

22
00:01:31,060 --> 00:01:34,740
And it actually doesn't matter which one you use using else.

23
00:01:34,750 --> 00:01:41,530
If so, another condition we're going to check to see if computer selection is less than or equal to

24
00:01:41,710 --> 00:01:43,410
zero point six seven.

25
00:01:43,780 --> 00:01:48,160
So we know that the first one is checking to see if it's less than this third.

26
00:01:48,550 --> 00:01:51,970
And if it's not, then we're going to check for the next third.

27
00:01:52,120 --> 00:01:58,810
And we already have pulled out the selections that match less than zero point three four.

28
00:01:58,990 --> 00:02:01,840
So we've already got that third listed as rock.

29
00:02:02,020 --> 00:02:03,580
So we only have a third left here.

30
00:02:03,580 --> 00:02:04,950
And the point, six, seven.

31
00:02:05,260 --> 00:02:12,490
So using that same formula, we know that if it lands on this, then we can make it another one of the

32
00:02:12,490 --> 00:02:13,090
options.

33
00:02:13,330 --> 00:02:15,550
And for this time, let's just do paper.

34
00:02:16,330 --> 00:02:22,420
And if none of that criteria matches of computer selection, it doesn't match any one of these, then

35
00:02:22,420 --> 00:02:26,170
we're going to simply select computer selection to scissors.

36
00:02:26,320 --> 00:02:34,960
And this will give us roughly around a 30 percent chance, a 33 percent chance on any one of these items

37
00:02:34,960 --> 00:02:36,390
for the computer selection.

38
00:02:36,400 --> 00:02:38,290
So I'm going to take that computer selection.

39
00:02:38,560 --> 00:02:39,910
I'll move it down here.

40
00:02:40,120 --> 00:02:47,320
And we know that the user has the entire text so that one is selected by the user and the computer selection

41
00:02:47,320 --> 00:02:48,790
is going to be at random.

42
00:02:49,030 --> 00:02:49,960
So we hit rock.

43
00:02:50,290 --> 00:02:51,460
Both of them got rock.

44
00:02:51,700 --> 00:02:55,090
If we had paper, the computer got scissors, we got paper.

45
00:02:55,300 --> 00:02:59,410
If we picked scissors, then we know that the computer got paper.

46
00:02:59,680 --> 00:03:00,900
So these are all at random.

47
00:03:00,910 --> 00:03:04,690
So it doesn't mean that if I hit scissors, it's always going to be paper.

48
00:03:04,690 --> 00:03:07,090
And that instance that seemed to occur that way.

49
00:03:07,210 --> 00:03:12,330
But we see sometimes we had scissors and then we get the computer returning back rock.

50
00:03:12,340 --> 00:03:18,600
So that's how you can introduce a randomness into the selection process, into the gameplay.

51
00:03:18,880 --> 00:03:23,740
And this is, again, a really crucial part of almost any game that you're going to be developing within

52
00:03:23,740 --> 00:03:24,590
JavaScript.

53
00:03:24,640 --> 00:03:32,410
So go ahead and add this in using conditions you can check to see what the value is and then update

54
00:03:32,410 --> 00:03:34,750
the computer selection value accordingly.

55
00:03:34,990 --> 00:03:40,540
And then we're going to be ready to move on to the next step where we're going to check to see who wins.

56
00:03:40,770 --> 00:03:46,990
That's, again, really important part of the gameplay where we need to have some conditions for winning,

57
00:03:47,440 --> 00:03:49,870
whether it's the player or the computer.

58
00:03:50,380 --> 00:03:51,240
So that's still to come.
