Buckys C++ Programming Tutorials - 27 - Random Number Generator

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys it's Bucky and welcome back to your 27th C++ tutorial and this tutorial is actually going to be a pretty fun one because we're going to be talking about random numbers what they are you know a couple functions that are going to help us build a random number generators and I'm 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 and the header file is called C STD Li B from the C standard library and we include this because we want to use a function from it called R and not run Rand and what this does is whenever you call this it basically returns to you a random number so let's see let's run it and we got 41 in this case so as you can see whenever we print out R and it pretty much takes a random number and gives it to you sorry saying Wow there you go there's our random number generator perfect well they're actually a couple problems with the rand and we're going to be talking about those later on but for now I want to show you guys how to build a basic random number generator and let's go ahead and just call Rand like 25 times so let's go ahead and make a for loop and go ahead and make an x set equal to 1 and make X is less than 25 and of course X plus plus so now we have a loop that's going to run 25 times or sometime around that so now let's just go ahead and see out umm well let's just go ahead and see out Rand and see what happens and then might as well end that line so it looks so nice and pretty let's go ahead and print it and see what we get okay so it looks like we're getting a huge list of random numbers all right looks pretty good but you know what if I'm going to build 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 R and 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,000 or something divided by 6 and give you the remainder of that so the remainder whenever you divide by 6 can either be 0 1 2 3 4 or 5 so let's go ahead and take a look at that whenever you divide by 6 you're limited to only the number 0 1 2 3 4 & 5 you can't have 6 as a remainder because anything divided by 6 with 6 remainder it would just be another one so you can say all right well I'm trying to simulate a dice here and they have the numbers 1 through 6 not 0 through 5 so we got 6 numbers but we don't want 0 through 5 we want 1 through 6 so Bucky can you just show me how to take all these numbers and shift them add 1 to them well that is very simple just go ahead and surround your R + 6 and shift put 1 plus now this is going to do is it's going to take a random number divided by 6 and whatever the remainder is which would be 0 through 5 it would add 1 to it so now you're giving 1 to 6 so now you can see that 0 is eliminated and we have 6 now so 1 2 oh let me find it three three four five and six so that is how you build a random number generator using you know a specific result set so remember however many numbers you want you put as a modulus of R and and however many you want to shift to by just add that many to it but let's go ahead and take a little closer look at this random number generator okay let's read these numbers five six five one one five okay they look like you know they're pretty random to me let's go ahead and run it again five six five one one five that seems kind of familiar let's go ahead and run this one more time five six five one one five all right what's going on with our random generator we built this program perfectly which we did so I mean we use a random RA everything is good to go so what's the deal why did it give us the same numbers every time well in order to understand this I need to talk to you guys about computers and random numbers now listen very closely because this is actually very important no computer 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 whenever you go into a casino and you see those automated slot machines and it looks like people are just spinning and skipping them random numbers well the truth is that it's not actually given them truly random numbers those slot machines even though they're supposed to be random number generator there have to follow a certain aggregate rhythm a certain pattern and you know whenever you're looking at like game shows or whenever you buy electronic and a flash like random lights well those lights are not random either everything in electronics that is supposed to be random is not random it's a computer it has to follow a complex algorithm in a complex pattern now the reason that these are so complex is that's what gives the appearance that it's random to humans we don't understand this algorithm right away only the computer does so whenever we see numbers like this five six five one one five three six those are random numbers to us but the computer it's really just following a big algorithm so we're saying all right so whenever we're building games and stuff we don't want our numbers to be predictable so what can we do in order to change that algorithm in a little bit well let's go ahead and let me include another no I wanted this yet but the rand function right here runs on a very complex algorithm and I don't know what the algorithm is and I don't think many people do but it's basically a large function that computers excuse me that humans aren't meant to understand so what C++ the developers allowed us to do is seed a random number and you use that in a function called s Rand 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 six two one six one six now let's go ahead and pass another one like 215 and now we get you know four one three one one or whatever so whenever we see it around a number we throw you know any old random number in there and it changes the algorithm so now I don't even know you know what happens when I type in 43 I get truly random numbers so if you're saying all right so let's go ahead and put 43 in there every time and we get four four one five one okay now let's run again four four one five one OMG Bucky what are you doing all you did is you change the algorithm a little bit and you know we didn't really make it a truly random number January we just gave a different algorithm and follow well all right I'll show you guys one last trick in in order to do this we need to include another header fire and and this is called see time and what this does is 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 and as a parameter of time not tema time go ahead and pass in zero now let's go ahead and run this and see what happens before I explain it okay two five two two okay let's run again four six one for sweet and one more time five four six four six so now we have a random number generator that works perfectly so now let me tell you guys why we know that we can use s R and to change the algorithm a little bit but if we just threw the same number in here every time oh just you know have a different algorithm but with the same results every time so what time zero does is time zero calculates the seconds sense like 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 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 and 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 is truly random or they're not truly random but there is random as we can make them so again what we did is here's the algorithm all ran does is it runs a complex algorithm s R and allows us to throw a number in there to change the algorithm a little bit and time zero counts the number of seconds and this value is changing every single second so that's 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 files because you can't use this program without it and there you go I'll see you guys next time
Info
Channel: thenewboston
Views: 554,633
Rating: 4.9547954 out of 5
Keywords: tutorial, beginners, game, class, program, pointers, lesson, thenewboston, bucky, roberts, computer, string, vector, download
Id: naXUIEAIt4U
Channel Id: undefined
Length: 9min 53sec (593 seconds)
Published: Sat Apr 09 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.