1 00:00:00,210 --> 00:00:07,200 Awesome, we can display the next person on the previous person and of course, let's work on the random 2 00:00:07,200 --> 00:00:08,410 person functionality. 3 00:00:08,820 --> 00:00:12,590 I think I'm going to come up right away with a function. 4 00:00:12,900 --> 00:00:14,610 So we're going to go with CONSED. 5 00:00:14,630 --> 00:00:18,930 And of course, we'll mindless, random, random person. 6 00:00:19,440 --> 00:00:22,750 Now, that is going to be equal to, of course, my natural function. 7 00:00:23,040 --> 00:00:29,190 Now, I'm not going to pass anything inside of the function, but just to showcase how the math random 8 00:00:29,190 --> 00:00:35,370 works, we're going to go along and the math and then random and will right away invoke it. 9 00:00:35,700 --> 00:00:40,830 And now, of course, where I have my button, I'm just going to go with on click not is going to be 10 00:00:40,830 --> 00:00:43,080 equal to my random. 11 00:00:43,380 --> 00:00:49,050 So now of course I'm going to navigate a bigger screen and then in a console you'll notice that as you're 12 00:00:49,050 --> 00:00:57,090 clicking on random person, you get a random value and that value always will be between zero and one 13 00:00:57,090 --> 00:00:57,790 hour. 14 00:00:58,050 --> 00:01:01,140 It can be zero, but it will never be one. 15 00:01:01,500 --> 00:01:07,530 So it can be zero point nine nine nine nine nine nine nine nine nine nine nine nine, but it's never 16 00:01:07,530 --> 00:01:08,650 going to be one. 17 00:01:09,160 --> 00:01:16,310 So how we can use this random value to get the random person from the array? 18 00:01:16,600 --> 00:01:19,340 Well, there's multiple ways how you can do that. 19 00:01:19,350 --> 00:01:27,530 But one of the ones that I like the most is simply by multiplying the value with a length of the array. 20 00:01:28,660 --> 00:01:35,650 And then just make sure that number is always around it down, so we go here with LET and then I'll 21 00:01:35,650 --> 00:01:37,150 call this random number. 22 00:01:37,170 --> 00:01:42,760 Now I'm purposely using a lot and you'll see in a second why and that is going to be equal to a mouth 23 00:01:42,760 --> 00:01:43,240 random. 24 00:01:43,480 --> 00:01:51,700 So first, what I could do is multiply by the length of my array, which of course in my case is going 25 00:01:51,700 --> 00:01:53,590 to be four because I have four items. 26 00:01:53,920 --> 00:01:59,380 So then if we navigate here now and by the way, I didn't cancel my bad. 27 00:01:59,400 --> 00:02:01,450 So we go here with a random. 28 00:02:02,300 --> 00:02:10,730 No, here, and you'll notice that as I'm clicking now, I'm getting a random number between a zero 29 00:02:11,030 --> 00:02:11,990 and four. 30 00:02:12,460 --> 00:02:18,890 So the thing is, it is never going to be really four because again, I will never have one. 31 00:02:19,130 --> 00:02:25,970 So if I multiply zero point nine nine nine something by four, it's always going to be somewhere between 32 00:02:25,970 --> 00:02:29,480 three and then four, but it's never going to be four. 33 00:02:29,900 --> 00:02:33,810 And what I would want to do instead is just always round it down. 34 00:02:34,520 --> 00:02:39,640 So then the values will be between zero and three. 35 00:02:40,340 --> 00:02:46,790 So essentially they're just going to give me all the indexes that I have in a row since I have four 36 00:02:46,790 --> 00:02:47,270 items. 37 00:02:47,630 --> 00:02:53,060 I have indexes between zero and three zero one, two and three. 38 00:02:53,410 --> 00:02:55,100 This is exactly what I'm going to do. 39 00:02:55,460 --> 00:02:57,440 So I'm going to have a math. 40 00:02:58,330 --> 00:03:06,130 Math on the floor, and I'll pass in this value so I can multiply the random number by the length of 41 00:03:06,130 --> 00:03:13,540 my array and I always make sure that I round it down so that just going to give me this random index. 42 00:03:13,780 --> 00:03:15,430 So, again, if we now get here. 43 00:03:16,560 --> 00:03:24,420 If we keep on pressing notice how we're getting values between zero and three, not one issue that people 44 00:03:24,420 --> 00:03:30,090 have complained about is the fact notice, even though technically it is a random number, once you 45 00:03:30,090 --> 00:03:32,450 click, you get some repetition. 46 00:03:32,700 --> 00:03:40,050 And essentially what that means is that as you're clicking the button, technically, if the numbers 47 00:03:40,050 --> 00:03:44,520 are exactly the same, well, the person won't change over here. 48 00:03:44,820 --> 00:03:51,870 And in order to avoid that, what you could do is simply say, you know what, if the random number 49 00:03:52,110 --> 00:03:59,670 is equal to my current index, well, then just add one or, I don't know, subtract one, whatever 50 00:03:59,670 --> 00:04:00,330 you want to do. 51 00:04:00,580 --> 00:04:02,310 So here, I'll say if. 52 00:04:03,180 --> 00:04:11,910 And then random, no, not person, but around them and the number, if it is equal to an index, then 53 00:04:11,910 --> 00:04:14,070 either add one or subtract one. 54 00:04:14,100 --> 00:04:14,790 That's up to you. 55 00:04:15,080 --> 00:04:19,860 You say random number is equal to index plus one. 56 00:04:20,160 --> 00:04:26,690 And then, of course, I would want to set index, but make sure that you also go with it. 57 00:04:26,970 --> 00:04:34,440 Check number where if you're adding this plus one, you still might run into this problem where the 58 00:04:34,440 --> 00:04:39,210 value might be bigger than the index for the last item in the array. 59 00:04:39,480 --> 00:04:41,580 So let's go with that index. 60 00:04:41,820 --> 00:04:48,930 But instead of just passing random number, we also need to go check number and then only pass this 61 00:04:49,140 --> 00:04:50,190 random number. 62 00:04:50,460 --> 00:04:58,080 And that way what happens is that you'll successfully avoid that repetition where if I go with random 63 00:04:58,260 --> 00:05:04,530 numbers, how each and every time there's going to be a random person, hopefully you're able to complete 64 00:05:04,530 --> 00:05:05,100 the project. 65 00:05:05,400 --> 00:05:07,530 And I hope to see you in the next project.