Welcome back.
In this video, I want to show you how you can create a random number.
And we're going to use that in order to build a little software that is called guess the number.
So the idea is that the computer has a number in mind, a random number.
And the user can then try to guess that number.
Okay.
So how can we create a random number.
Therefore we need to use the random class.
So there is this class called random.
And what we're doing here is we are creating an instance of the random class.
So what is an instance and what is a class.
These are things that we're going to cover a little later on.
But the general idea is that inside of the entire.
Net framework, we have a bunch of different classes that we can use, and we are already using one
of those classes all the time, and that is the console class.
So this console class, as you can see here, it says class has a bunch of methods that we can use in
order to then work with it.
And by default, we don't need to create an instance of console in order to work with it.
Otherwise we would have to go ahead and say something like console for example, my console is equal
to new console, okay, like so, but the console is already made available to us directly, so we don't
need to do that.
But with other classes such as with the random class, we need to do that.
So just follow along.
Now this will become a lot clearer once we actually get into classes and objects and creating instances
of classes and so forth.
But for now, I wanted to just show you a few lines of code that will allow you to create a random number
or generate a random number.
So here I'm going to create an int random number.
And that will use this random dot next.
And then inside of those brackets I can tell what range I want to have.
So let's say I want to have a number between 1 and 10.
Then I would have to enter one and 11 because the max value is exclusive.
So the exclusive upper bound of the random number returned max value must be greater than or equal to
min value.
So this just means that this 11 will not be included.
It's excluded.
So this will give us a random number.
This will give us a random number between 1 and 10.
Okay.
So I can just say guess the number now okay.
Because we do have a random number.
And now what we can do is we can get the user input.
And then if the user actually did enter a number, we can compare if the number that the user is has
entered is the same as this random number.
Okay.
So this will now contain a random number each time that we run our software.
So let's check if the user entered the right number.
And then we check if num one equal to random number.
Then we're going to say c w.
So console right line you guessed right.
And otherwise we're going to say you guessed wrong.
Try again okay.
So now we're using this nested if statement.
And we pretty much have our guest number application.
So let me scroll out a little bit so you can see it all.
This is our app.
Obviously we need to have this read key here at the bottom as well so that our software stays alive
okay.
So this is our software.
Very simple.
And let's look over what we do.
So first we create an instance of the random class so that we can use its next method which will give
us a random integer that is within a specified range.
So that's what this next method does.
And it stores it inside of our int called random number.
Then we're asking the user to enter a number.
We are converting that number that the user has entered the string, so to speak.
We're converting that into an integer here.
And we're if it worked then we say is numbers true?
If it didn't work then we say is number is false.
So if it did work we're checking if the number that the user has entered is the same as random number
okay.
So if that's the case then we say you guessed it right.
And if you guessed it wrong then we say you guessed it wrong.
Try again.
So let's run our software and I'm going to say five.
And you guessed wrong, try again.
Well, obviously we don't have a way of running through this again and have a second try.
And that's where loops would come in handy.
Okay.
And loops will be the next big topic that we're going to cover in the next chapter.
So in our case we need to run it again.
But you know what.
As a developer, I can cheat because I can just add a breakpoint here and I can just know what the random
number will be.
So let me run this again, and this time I'm going to hover over random number.
Actually I can see it down here as well.
At the bottom I don't need to hover over the variable, but it's either way both works.
So I can see random number is two.
So now let me go in or like go to the next step and actually let me continue okay.
And now my app will ask me for the number I know that it's two.
So I'm going to press two.
And it says you guessed it right.
Guess what?
I'm the developer so I can cheat the system.
I can hack it so to speak.
Okay, so this is pretty much how you can build your simple little guess the number right?
Application.
However, it only really comes to life when you can guess maybe a second or a third time.
And that's something that we're going to add in the next chapter.
So be prepared for some really cool stuff for loops, while loops do while loops all the good loop stuff.
So see you in the next video.