Please wait a little bit to Load
So if we say see how name diet size when we run that it will print out the size of name which has four characters at that same token.
There’s another function called named Leath which will do the exact same thing. Now print out four. So like I said there are several string functions that you can use and if you look through you I have to do is type in name die and all these functions pop up the way that you can see. And if you just play around with them you know you can kind of see what they do. For instance let’s just use fun.
We use dot found and will search for see and I believe if we see out in that if it runs it will return to because that’s the position of C in the string because remember zero warning too. And if we type in h here if we find h it will return 3 and that’s what the found function does. You can find certain characters throughout the string. But if we tap in a character that isn’t in the string such as the J it should return negative 1 or a value such as this some kind of garbage value. Because obviously that that’s not a position anywhere in the string. So you could for instance type in Y and you’ll get another straight edge value. So we get another strange value and that’s kind of how you can decipher whether the whether the character was found or not.
So that’s basically all I kind of wanted to show you was that each of these name objects has or each of the string object variables have their own built in functions that you can use such as font size. We replace in you know they’re all down here you can kind of stroll through them. Look at all of them.
Now. We will be discussing how to create our own random number generator. Now our random number generator is is it’s a function that returns a random number so that we can use it in our program. And the reason why we went to cover this is because believe it or not this is something that many people like to figure out how to do so that they can incorporate it into games or certain programs that require some level of randomness. Now if if you if you’re just trying to kind of figure out how to make a random number on her own you would have to kind of create your own algorithm and be quite a lengthy process.
So what I recommend doing as a beginner C++ programmer is anytime you’re looking for some kind of kind of functionality like such as randomness for example like we’re doing here I recommend going to C++ dot com as you can see up here and just searching for white what you’re looking for. And in this case I typed in random and I ended up with this function called Raymund and you can see it here is called it Rand void. And if you kind of just look through these documents you can see how you can use this library and these libraries to create a random number generator. And it’s really quite simple.
And they spell it out for you right here how easy it is to get your program to spit out a random number. Well about as random as you can get any ways. You know all computers. There’s no way to really make them completely random but you can at least make it appear in them to the user.
So that’s exactly what we’re going to do. You can kind of mark this reference down if you want to come back and read it later. But basically in this program all we’re going to be doing is everything that this reference page tells us to do to create our generator. So let’s go back to our program and we’re first going to include the libraries that we need for our random number generator to work.
And that is include standard library h file and then include time h file. You might be wondering what this Tom-Kat h libraries for. Well our random number generator is going to be based off the internal clock of the machine and it’s going to incorporate that into its algorithm to come up with a random number and you’ll see what I mean here in a second. I mean it won’t be extremely clear but that’s basically how this algorithm works is that it gets the current town down to the millisecond and throws that into a function. And basically that function is going to spit out a different number every single time because the time is changing constantly and depending on what the actual time is the algorithm may spit out a completely different number than the one is spit out one millisecond ago.
So that being said let’s go ahead and create a function that’s going to generate our number will have a return an integer value because we want to return an integer or call it generate random number and won’t take any arguments and then down here we’re actually going to write out our functions.
All right same thing generate random number and then in here is where we will write our code.
And if you go to C++ dot com and look at it it’s actually quite simple. You just write in your libraries and then write this small function right here which initializes the random seed to the internal clock on the computer and then you just simply spell out your name your variable with this function with this number always mean the number between 0 and then. And this number to be all your random values that are possible and then plus 1 and this will return any number.
For instance right here this secret variable will spit out any number randomly between 0 and 10 because they have 10 specified right here. So let me show you what I mean. So the first thing we have to do is type x ray and parentheses in and these parentheses for the constructor the type time. And then another constructor as Knodell and a semi-colon. And just like that and that may seem really unintuitive at first but that’s what the C++ dot com reference tells us to do. So that’s exactly what we’re going to do for a random number generator. And on certain functionality that you might need in your program it’s not that important to understand exactly how it works you just need to know how to use it.
And that’s kind of what I’m showing you right here. So this is how you would use the random random number generator library with a tomcat age and standard library header files. So now let’s go ahead and hold a we’re going to hold a value we’ll just return. There’s so there’s two ways to do this so you can create a value called the net and we’ll just call it number.
I will set it equal to Rand but your constructor modulo operator the number between 0 and 5th and 0 and then x basically that you want the highest number to be we’ll put 50 down. And then plus one semi-colon and this number when this code Rymes will be any number between 0 and 50 on a basically picked randomly. So for the return number there. And then here we just return we call the function generate random number when we run the program. The main we’ll call that function and we see processor turn 41 because it’s calling in this return function of the main it’s calling generate random number and when it returns the value returned from generate random number the returned 41.
But if we run it again it will give us a different number. This time it returned 50 but we can keep running this over and over again and every time it will be a different number between 0 and 50 and all we have to do if we want to change the spread we get changes to 200. For example there will be any number between 0 and 200. This time it was 96. So that’s how you use a basic random number generator. I just kind of wanted to go over it with you so that you would know what we were doing when we make our final project.