Why Random Numbers Aren't Random

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome to another youtube video so in today's video i'm going to show you why random numbers are not so random what i mean by that is that random numbers in most computer programming languages are actually generated deterministically that means that you can predict exactly what numbers will be generated randomly from whatever generates the random number and you can actually control it as well now i'm going to show you a few examples here in python but you can likely try this in other programming languages as well and you'll see that the sequence you're getting is not quite random now there is a lot of techniques used to make it seem random but really what's actually used behind the hood of python and other languages when generating random numbers is a pseudo random number generator so that means it's not a real random number generator and it's not truly truly red alright so with that said let's dive into the video but first i need to talk to you about something that's not random which is the sponsor of this video before we get started i need to thank the sponsor of this video which is alco expert algo expert is the best platform to use from preparing for your software engineering coding interviews and has the highest quality coding interview practice questions with 160 practice questions detailed solutions in nine of the most popular programming languages a feature-packed browser-based coding environment extensive test suites and conceptual overviews and code walk-throughs for each and every problem algo expert is the best resource to use to ace your coding interviews algoexpert also has a data structures crash course coding interview assessments and a mock interviews feature i can highly recommend algo expert as a former customer myself and now an official instructor on the platform get started using algo expert today by clicking the link in the description and using the code tech with tim for a discount on the platform all right so let's go ahead and dive in now first thing i want to mention here is that if you want to read more about this i will leave a link in the description to the random module from python documentation so you actually see it says generate pseudo random numbers the term pseudo tells you these are not truly random numbers and well you can go through here and this pretty much explains what i'm going to recap here in this video anyways let's go back to this screen and let me show you what i mean by being able to generate the same sequence of random numbers and why the numbers are not quite random so i'm in python so i'm going to import the random module this has a bunch of functions allowing you to generate distributions random numbers random choices of elements all kinds of stuff like that regardless what i will do is say 4 underscore in range and then i'm going to go with 10 and i'm just going to print out random dot rand range and we go with 0 to 10. so this would generate a random number in the range of 0 to 10 but it won't give us 10. so to go 0 to 9 10 is kind of the upper bound which is not included anyways let's just run this and notice here this is the sequence that we get so i'm going to copy this sequence and i'm going to run this script one more time and just show you that we don't get the same sequence again right so this sequence clearly does not match this sequence and so at first glance it seems like okay it is actually giving me a random number however i'm going to show you that i can make it so it gives me the exact same sequence and the way i'm going to do that is by using this method called seed so i'm going to say random.seed i'm going to pass a seed of 1. now some of you may already know what's going to happen here but let's clear this out and let's run this so this is the sequence that we get of 10 numbers okay i'm going to put it inside of quotation marks like this and now i'm gonna run it again now notice when i run it again i get the exact same sequence now i'm not lying to you i actually am re-running the code you can see that it's flashing and i'm re-running the code a bunch of times and every single time i rerun it i get the exact same sequence why is that well the reason why i'm getting the same sequence is because i have the same seed so you can actually control the random sequence that you get based on a seed and the reason why that's the case is the seed is actually the starting value of generating random numbers and the way that a pseudo random number generator works is it starts at a specific value and performs deterministic changes to that value to give you some random value and so whenever you're starting at the same seed you always get the exact same random sequence now this might actually sound really familiar especially if you've ever played the game minecraft a lot of you have probably played minecraft and you've seen that you can import or give a seed and every time you use the same seed you get the exact same map and this works the exact same way you give a seed the seed is what generates the actual random sequence really it's kind of the starting point of it and so if you put in the same seed somewhere else you get the exact same map same thing here when we're generating random numbers in python now let's just do one more example let me change the seed to 2. now notice when i change it to 2 we get an entirely different sequence but if i keep running this i get the exact same sequence so how does the computer actually generate a random number then what does it use for this seed if it can't generate something truly random how does it pick something random for the seed well the truth is what's actually used for the seed by default is the system time so whatever the current time is when you run the code is what's passed to this seed so that it's always different the time is always different but again if you ran the code at the exact same system time like to the exact same millisecond you would get the exact same random sequence all right so now that we've looked at that let's actually look at an example where we use the system time as a random seed now this will kind of simulate what goes on behind the scenes just so you can get an idea of what the random module actually does when it's giving you random numbers so let's do this let's import time now to get the system time in python you do time dot time now this doesn't give you like the date time this gives you i believe the number of seconds past the specific date so there's some date in which they like just picked arbitrarily i guess to do this from but it's something like the number of seconds past like march 2nd on 1979 i'm completely making up that number or that date there but it's the number of seconds past a specific date so if i print this out and i go print time.time like this you'll see that we get this right so just like a huge number it's not telling me you know it's january or february or whatever the month is just giving me a number anyways if i use time.time i can put it like this now every single time i run the code let's just get rid of this here i'm going to get a different sequence so i get a different sequence because the seed is different and the time is constantly changing so that's the way that python actually does this if you don't manually set the seed of the random generator then it just uses the time and obviously we don't need to set this if we just do this right it continues using the time perfect so that's what happens when you use the system type so now that we've talked about this i'm just going to take a minute to discuss how do you actually generate something that's truly random because in a lot of cases this is no good using a pseudo random number generator can actually lead to a lot of security issues and this is really important especially if you're talking about something like cryptography which relies on the fact that random numbers are not guessable they're not predictable now in this instance it is completely predictable and so if you know the starting point of the sequence you're able to generate the random sequence you can do the entire thing to infinity as long as you know the starting point so when you're actually trying to generate something random you need to pick some external source from outside of the computer to use as your random seed or to use to generate randomness so an example of this which seems really extreme is actually radioactive decay so in a lot of instances when you're trying to generate a super random number a real random number you actually will monitor an atom and check for the radioactive decay of that atom and that is something that is random in the universe you can't predict when the decay is going to be you can predict it to you know a minute or to some level of precision but you don't know exactly when it's going to occur so a lot of computer systems that need to generate truly random numbers will observe something random in the universe like an atom and look for decay in that atom or more naively they'll use something like a mouse so as my mouse is moving around the screen this is a completely random pattern you can't predict how i'm moving the mouse and so that's something you could use to generate randomness as well or like the sequence in which i hit keys on the keyboard or the timing for that all kinds of things like that can be used to generate randomness and as a seat now i'm no expert on this topic i'm just giving you you know the little bit of knowledge i have from reading about this for a few days but i thought this was interesting and i figured i would share with you in case any of you find it interesting as well with that said if you enjoyed the video make sure to leave a like subscribe to the channel and i will see you in another one [Music]
Info
Channel: Tech With Tim
Views: 27,872
Rating: 4.9528446 out of 5
Keywords: tech with tim, random numbers, generate random numbers, system time, seed, random seeding, seeding, seed state, vector, pseudo number, pseudo random number, coding, programming, generating numbers, using pseudo random numbers, pseudo code, calling random numbers, python random number, random numbers aren't random, not random, radioactive decay, radioactive decay half life, truly random numbers, using random numbers, cryptosecurity, randomly generated numbers, cyber security
Id: Nm8NF9i9vsQ
Channel Id: undefined
Length: 9min 17sec (557 seconds)
Published: Wed Sep 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.