Welcome in his lecture, I'm going to create a game project called The Lucky Number.
The user will pick a number by passing it to the program from the command line, and the program will
use a loop to produce random numbers within a range.
And the last random number matches to the guest number, then the user will be.
I started this project into a few lectures first you're going to learn how to produce random numbers.
No, after that, I'm going to show you how to build the game.
All right.
Let's get started.
They want to produce random numbers, single unit, use a package called Randt this.
OK, let's take a look at its documentation.
I'm going to open up my browser and I'm going to type goaland that org package mat's all right.
This part is especially important producers, deterministic sequence of values each time a program is
wrong.
So this means that the round package doesn't actually produce truly random numbers, but they can look
like as if they're random.
This is what pseudo here means.
You can read more about it by following the link inside the resources of this lecture.
OK.
So basically, I can tell you that computers cannot generate truly random numbers, they are deterministic
machines, everything they do is actually predictable.
However, randomisation is the direct opposite of predictability.
So generating truly random numbers using computers is harder than you may think.
Fortunately, we can imitate it and you're going to learn how to do that.
And also here, it says that random numbers are generated by a source, so the generated numbers depend
on this source.
You'll see what this does mean in the example code in a minute.
All right, after this introduction, let's take a look at the function that I need.
There are a lot of functions here for producing random values, for example, this wonderful alternative
produces random flow, 32 values, or this one produces random for six to four values.
And so how are we going to produce a random integer within a range?
There are only a few functions here that produce random integers.
For example, let's take a look at this one here.
This can produce in a random integer value, but that's not what I want.
I want my random numbers to be in the range, so let's go back.
This in here is actually what I need.
Let's take a look at it.
It takes an entire value and returns a positive random integer value between zero and.
And is the value that I'm going to pass into this function.
Cool.
So these are the turns around them, integer within a range.
Perfect, this is a quiet race here, by the way, means that it includes the zero and this closing
parenthesis here means that it doesn't include the N.
For example, if I pass 10 to this function, it would return a random number between zero and nine,
but it won't include the ten because of that.
I'm going to pass it 11 instead of 10.
All right, this all will be clear soon, I promise.
Now let's start cutting.
First, let's declare a variable like this, yes, let's assign OK, this variable will save the gas
number.
Now, let's create a loop.
Let's declare the rabble here, I'm going to put a random number into this variable inside the loop.
Now, let's add a condition like this and nothing else to guess, OK, so it will loop until it finds
the number that our guests or the user guesses.
Now, let's generate a random number using the term function like this.
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
plus vontez, right?
So why pass guess plus one instead of just gas?
Remember, that intern returns a random number between zero and.
But I wanted to return also the number that our guest, if I didn't do so, it wouldn't include the
guest number.
That means it would only return a random number between zero and nine instead of zero on time.
So that's why I added plus one to it.
OK, now let's print this random number like this.
I'm going to call printf for integers de.
And I'm going to pass on, OK, OK, after the loop, just put another print on here to print a new
line to make things look better.
All right, ready?
Run it.
Let me read it a few more times, as you can see it all this produces the same set of numbers.
This is because Gore uses a random generator, not a truly one.
So how can we generate Ever-changing random numbers?
This, take a look at the ongoing documentation of the round packaging.
Now, here's a function called Sete.
Let's take a look at it.
Seat uses the provided seat value to initialize the default source to a deterministic state was really
clear, isn't it?
I think that this documentation needs to be updated anyway in English.
What is actually trying to say is that, for example, if you see that turn, it will produce some set
of numbers, or if you see that with hundred, it will produce another set of numbers.
So to see the function here determines the variations of the random numbers.
This is exactly what I need.
Let me show you an example by using this function.
Let's call Randazza, here we turn.
For example.
OK, let me run it a few times.
OK, now let's see it tweet hundreds instead.
Let's run again.
As you can see, the random numbers are entirely dependent on which seat number is used, right?
So different seat means different set of random numbers, but I need to generate a different set of
random numbers each time the program runs so that the user will actually have a chance to win or lose
the game.
To do that, I need a number that changes all the time, then I'll use that number as a seat to get
a different set of random numbers each time the program runs.
Let me ask you this.
What is that thing that is constantly changing in our universe?
Time, right, yep, time, OK, in the next picture, I'm going to show you how to use time to see
the random number generator so see in the next picture by.