WEBVTT 00:02.350 --> 00:07.090 Welcome in his lecture, I'm going to create a game project called The Lucky Number. 00:08.090 --> 00:13.940 The user will pick a number by passing it to the program from the command line, and the program will 00:13.940 --> 00:17.000 use a loop to produce random numbers within a range. 00:17.600 --> 00:21.730 And the last random number matches to the guest number, then the user will be. 00:22.810 --> 00:28.360 I started this project into a few lectures first you're going to learn how to produce random numbers. 00:28.390 --> 00:31.810 No, after that, I'm going to show you how to build the game. 00:32.380 --> 00:32.800 All right. 00:32.800 --> 00:33.430 Let's get started. 00:35.710 --> 00:40.730 They want to produce random numbers, single unit, use a package called Randt this. 00:42.520 --> 00:44.560 OK, let's take a look at its documentation. 00:45.340 --> 00:52.840 I'm going to open up my browser and I'm going to type goaland that org package mat's all right. 00:54.500 --> 01:01.160 This part is especially important producers, deterministic sequence of values each time a program is 01:01.160 --> 01:01.470 wrong. 01:02.330 --> 01:08.240 So this means that the round package doesn't actually produce truly random numbers, but they can look 01:08.240 --> 01:09.440 like as if they're random. 01:09.800 --> 01:11.870 This is what pseudo here means. 01:12.320 --> 01:16.280 You can read more about it by following the link inside the resources of this lecture. 01:16.310 --> 01:16.670 OK. 01:17.920 --> 01:24.070 So basically, I can tell you that computers cannot generate truly random numbers, they are deterministic 01:24.070 --> 01:26.910 machines, everything they do is actually predictable. 01:27.400 --> 01:30.910 However, randomisation is the direct opposite of predictability. 01:31.800 --> 01:36.120 So generating truly random numbers using computers is harder than you may think. 01:36.570 --> 01:40.610 Fortunately, we can imitate it and you're going to learn how to do that. 01:42.560 --> 01:49.070 And also here, it says that random numbers are generated by a source, so the generated numbers depend 01:49.070 --> 01:49.840 on this source. 01:50.510 --> 01:53.590 You'll see what this does mean in the example code in a minute. 01:56.930 --> 02:00.980 All right, after this introduction, let's take a look at the function that I need. 02:03.870 --> 02:09.390 There are a lot of functions here for producing random values, for example, this wonderful alternative 02:09.430 --> 02:15.640 produces random flow, 32 values, or this one produces random for six to four values. 02:15.660 --> 02:20.340 And so how are we going to produce a random integer within a range? 02:20.790 --> 02:23.970 There are only a few functions here that produce random integers. 02:25.190 --> 02:27.290 For example, let's take a look at this one here. 02:28.160 --> 02:32.300 This can produce in a random integer value, but that's not what I want. 02:33.480 --> 02:37.350 I want my random numbers to be in the range, so let's go back. 02:39.070 --> 02:41.680 This in here is actually what I need. 02:42.400 --> 02:43.290 Let's take a look at it. 02:44.590 --> 02:50.010 It takes an entire value and returns a positive random integer value between zero and. 02:50.880 --> 02:53.670 And is the value that I'm going to pass into this function. 02:54.450 --> 02:54.690 Cool. 02:54.840 --> 02:57.510 So these are the turns around them, integer within a range. 02:58.750 --> 03:05.080 Perfect, this is a quiet race here, by the way, means that it includes the zero and this closing 03:05.080 --> 03:08.500 parenthesis here means that it doesn't include the N. 03:09.250 --> 03:15.070 For example, if I pass 10 to this function, it would return a random number between zero and nine, 03:15.430 --> 03:18.700 but it won't include the ten because of that. 03:18.700 --> 03:20.770 I'm going to pass it 11 instead of 10. 03:21.770 --> 03:25.040 All right, this all will be clear soon, I promise. 03:25.370 --> 03:26.500 Now let's start cutting. 03:27.710 --> 03:36.050 First, let's declare a variable like this, yes, let's assign OK, this variable will save the gas 03:36.080 --> 03:36.560 number. 03:37.580 --> 03:38.810 Now, let's create a loop. 03:40.790 --> 03:46.700 Let's declare the rabble here, I'm going to put a random number into this variable inside the loop. 03:47.700 --> 03:54.510 Now, let's add a condition like this and nothing else to guess, OK, so it will loop until it finds 03:54.510 --> 03:56.880 the number that our guests or the user guesses. 03:58.080 --> 04:01.740 Now, let's generate a random number using the term function like this. 04:03.480 --> 04:10.350 That in turn, I'm going to pass guess that I'm going to add plus one to it's OK here, I passed gas 04:10.350 --> 04:11.440 plus vontez, right? 04:12.530 --> 04:15.410 So why pass guess plus one instead of just gas? 04:16.670 --> 04:20.420 Remember, that intern returns a random number between zero and. 04:21.400 --> 04:28.090 But I wanted to return also the number that our guest, if I didn't do so, it wouldn't include the 04:28.090 --> 04:28.900 guest number. 04:29.500 --> 04:34.610 That means it would only return a random number between zero and nine instead of zero on time. 04:35.290 --> 04:37.750 So that's why I added plus one to it. 04:37.810 --> 04:41.290 OK, now let's print this random number like this. 04:41.930 --> 04:45.130 I'm going to call printf for integers de. 04:46.080 --> 04:51.180 And I'm going to pass on, OK, OK, after the loop, just put another print on here to print a new 04:51.180 --> 04:52.830 line to make things look better. 04:53.580 --> 04:54.230 All right, ready? 04:55.040 --> 04:55.410 Run it. 04:56.910 --> 05:02.910 Let me read it a few more times, as you can see it all this produces the same set of numbers. 05:03.600 --> 05:07.920 This is because Gore uses a random generator, not a truly one. 05:09.240 --> 05:12.240 So how can we generate Ever-changing random numbers? 05:13.700 --> 05:17.120 This, take a look at the ongoing documentation of the round packaging. 05:18.700 --> 05:20.950 Now, here's a function called Sete. 05:21.930 --> 05:22.730 Let's take a look at it. 05:24.230 --> 05:32.200 Seat uses the provided seat value to initialize the default source to a deterministic state was really 05:32.200 --> 05:33.050 clear, isn't it? 05:33.640 --> 05:38.520 I think that this documentation needs to be updated anyway in English. 05:38.530 --> 05:43.990 What is actually trying to say is that, for example, if you see that turn, it will produce some set 05:43.990 --> 05:49.000 of numbers, or if you see that with hundred, it will produce another set of numbers. 05:50.320 --> 05:54.550 So to see the function here determines the variations of the random numbers. 05:55.920 --> 05:57.480 This is exactly what I need. 05:59.450 --> 06:01.820 Let me show you an example by using this function. 06:02.900 --> 06:06.320 Let's call Randazza, here we turn. 06:06.440 --> 06:07.070 For example. 06:09.600 --> 06:11.490 OK, let me run it a few times. 06:13.400 --> 06:15.900 OK, now let's see it tweet hundreds instead. 06:17.800 --> 06:18.550 Let's run again. 06:19.990 --> 06:24.920 As you can see, the random numbers are entirely dependent on which seat number is used, right? 06:25.540 --> 06:31.150 So different seat means different set of random numbers, but I need to generate a different set of 06:31.150 --> 06:37.540 random numbers each time the program runs so that the user will actually have a chance to win or lose 06:37.540 --> 06:37.930 the game. 06:39.230 --> 06:45.440 To do that, I need a number that changes all the time, then I'll use that number as a seat to get 06:45.440 --> 06:48.560 a different set of random numbers each time the program runs. 06:49.160 --> 06:50.210 Let me ask you this. 06:51.080 --> 06:54.500 What is that thing that is constantly changing in our universe? 06:56.980 --> 07:03.240 Time, right, yep, time, OK, in the next picture, I'm going to show you how to use time to see 07:03.280 --> 07:07.270 the random number generator so see in the next picture by.