What's going on guys? This is Bucky and welcome back to your 27th C++ tutorial. And this tutorial is actually going to be a pretty fun one, because we are going to be talking about random numbers. What they are, a couple functions. They are going to help us build random number generators. And I am actually going to teach you guys how to build a sweet random number generator. So, before we get into that, we need to include a header file. So, include in the header file is called "cstdlib", from the C standard library and we include this because we want to use a function from it called rand, not rund, rand. What this does is whenever you call this, it basically returns to you a random number. So let's see and let's run it. We got 41 in this case. So as you can see, whenever we print out rand(), it pretty much takes a random number, and gives it to you. So, you are saying "wow". There you go! There's our random number generator, perfect. Well, there's actually a couple problems with the random number generator and we are going to be talking about those later on. But, for now, um, I want to show you guys how to build a basic random number generator. And, let's go ahead and call rand like 25 times. So, let's go ahead and make a for loop. And go ahead and make int x equal to 1. And make x is less than 25 and of course make x plus plus. And now we have a loop that is going to run 25 times or some time around that. So now let's just go ahead and cout, and let's just go ahead and cout rand and see what happens. And my as well end that line. It's nice and pretty. Let's go ahead and print it and see what we get. ok, so it looks like we are getting a huge list of random numbers. Alright, looks pretty good. Uh, you know, what if you are building a random number generator and I don't want numbers quite this big in tens and thousands and stuff like that. Say we're building a random number generator to simulate people rolling dice or something. Well, I only want the numbers 1 to 6. So, here's a neat little trick where you can get that result set. In order to get a certain set of numbers, go ahead and take your rand and put modulus 6. What this is going to do is it's going to give you six random numbers. it's going to take a big number like 25 thousand or something, divide it by six and give you the remainder of that. So, the remainder whenever you divide by 6 can be either 0 , 1, 2 ,3 , 4, or 5. So, let's go ahead and take a look at that. Whenever you divide by 6, you are limited to only 0, 1, 2, 3, 4, and 5. You can't have six as a remainder because anything divided by 6 with six remainder would be just another 1 (actually 0). So, you can say alright. 2:51 Well, I'm trying to simulate dice here. And they have the numbers 1 through 6. Not zero through 5. So, we got six numbers. We don't want zero through 5. We want 1 through 6. So, Bucky, can you just show me how to take all these numbers and shift them? Add one to them. Well, that is very simple. Just go ahead and surround your rand six and just put 1 plus. Now what this is gonna do, is it is gonna take a random number. divide it by 6. And whatever the remainder is, which would be 0 through 5, and add one to it. So, now you're getting 1 to 6. So, now you can see that zero is eliminated. And, we have 6 now. 1, 2, oh let me find a 3, 3, 4, 5, and 6. So, that is how you build a random number generator, oh you know, a specific result set. So, remember, however many numbers you want, you put as in modulus of rand and however many you want to shift it by, just add that many to it. Well, let's go ahead and take a little closer look at this random number generator. ok, 3:55 let's read these numbers. 5, 6, 5, 1, 1, 5. ok, they look they are pretty random me. Ok, let's go ahead and run it again. 5, 6, 5, 1, 1, 5. That seems kind of familiar. Let's go ahead and run this one more time. 5, 6, 1, 1, 5. Alright! What is goin' on with our random number generator? We built this program perfectly! Which we did. So, I mean we used rand. Alright, everything is good to go. So, what is the deal? Why did it give us the same numbers every time? 4:25 Well, in order to understand this, I need to talk to you guys about computers and random numbers. Now listen very closely. This is actually pretty important. No computer or random number generator is truly random. The truth is that computers are not humans. Computers have to follow a certain algorithm, a certain set of instructions, a certain pattern. So, for example, whevever you go into a casino and you see those automated slot machines and looks like people are just spinning and it's givin them random numbers, well, the truth is this it is not actually giving them truly random numbers. Those slot machines, even though they are supposed to be random number generators, they have to follow a certain algorithm, a certain pattern. And, you know, whenever you are looking at game shows, or whenever buy an electronic and it flashes like random lights. Well, those lights are not random either. Everything in electronics that is supposed to be random, is not random. It is a computer. It has to follow a complex algorithm and a complex pattern. Now, the reason that these are so complex is... that is what gives it the appearance that it is random to humans. We don't understand algorithm right away. Only the computer does. 5:44 So, whever we see numbers like this: 5, 6, 1, 1, 5, 3, 6, those are random numbers to us, but to the computer, it is really following a big algorithm. So, if you are saying "alright!", so, whenever we are building games and stuff, we don't want our numbers to be predictable. So what can we do to change that algorithm a bit. So, let's go ahead and let me include... Well, I won't do this yet. The random function right here runs on a very complex algorihtm and I don't know what that algorithm is, and I don't think many people do. 6:20 But, it is basically a large function that computers, excuse me, that humans aren't meant to understand. So, what C++ the developers allow us to do is seed a random number. And, you use that in a function called srand(). Now, what this function does is it allows us to pass in a random number, any number we want like 67 and now whenever we build it, we get different results: 6, 2, 1,6, 1, 6. Now let's go ahead and pass in another one like 215. And now we get 4, 1, 3, 1, 1 or whatever. So, whenever we seed a random number, we throw, you know any old random number in there and it changes the algorithm. So, now, I don't even know what happens when I type 43. You get truly random numbers. So, if you are saying alright. 7:10 So, let's go ahead and put 43 in there everytime and we get 4, 4,1, 5, 1, 5. Ok, now let's run it again. 4, 4, 1, 5, 1, 5. oh, and gee Bucky what are you doing? All you did is you changed the algorithm a little bit. And you know, we truly didn't make it a random number generator. We just gave it different algorithm to follow. Well, alright! I will show you guys one last trick. And in order to do this, we need to include another header file. include, and this is called ctime. And what this does is it allows us to access the clock or the time in our computer. And now let's go ahead and pass in a function called time, not tima, and as a parameter of time, go ahead and pass in zero. Now, let's go ahead and run this and see what happens. Before I explain it. ok, 2, 5, 2, 2. Ok, let's run it again. 4, 6, 1, 4,1. Sweet! And once more time. 5, 4, 6, 4, 6. So, now we have a random number generator that works perfectly. 8:15 So, now, let me tell you guys why. We know that we can use srand to change the algorithm a little bit. But, if we just threw the same number in here everytime. It would have just a different algorithm, but with the same results every time. So, what time zero does is time zero calculates teh seconds since 1970 or something. And, what this does is every second this value is changing. So now it might be a million seconds. now, a million and one. now, a million and two. People don't have access to how many seconds are you know counting since 1970. So, this gives the appearance that this number generator is truly random now. So, this value is ever changing 9:01 That's why every single time we run our program, we're going to get a different result. So, that is how people build random number generators that are as truly random or they're not truly random, but they are as random as we can make them. So, again, what we did is, here is the algorithm. All rand does is runs a complex algorithm. srand allows us to throw a number to change the algorithm a little bit. time zero counts the number of seconds. And this value is changing every single second. That is why since this value is changing, this whole algorithm is changing every single second. And that allows us to make a truly random number generator. So, there you go. I hope you guys enjoyed this tutorial. Thank you guys for watching. And don't forget to include your header file, because you can't use this program without it. And there you go. I will see you guys next time.