Why Random Numbers Aren't Random

Tech With Tim · Intermediate ·📰 AI News & Updates ·4y ago

Key Takeaways

The video explains how random numbers in computer programming languages like Python are generated deterministically using pseudo random number generators, and how this can be controlled using a seed value. It also discusses the limitations of pseudo-random number generation and the potential security issues it can lead to, especially in cryptography.

Full Transcript

[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]

Original Description

Welcome to another video! In this video, I'm going to show why random numbers are not random. In most computer programming languages, random numbers are generated deterministically. Meaning you can guess what the random number will be and control it as well. 💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim 📄 Resources 📄 Python Random Module: https://docs.python.org/3/library/random.html ⭐️ Timestamps ⭐️ 00:00 | Why Random Numbers Aren't Random 01:58 | Random Module Documentation 02:23 | Generating Random Numbers in Python 03:30 | Random Seeding 05:45 | Using System Time As A Seed 07:11 | How To Generate Truly Random Numbers ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 💰 Courses & Merch 💰 💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 🔗 Social Medias 🔗 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear 🎬 🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9 🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV 📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r 🕹 Tripod: https://amzn.to/3hpSprv 🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc 🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl 🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z ☀️ Lights: https://amzn.to/2ApeiXr ⌨ Keyboard (Daskeyboard 4Q): https://amzn.to/2YpN5vm 🖱 Mouse (Logitech MX Master): https://amzn.to/2HsmRDN 📸 Webcam (Log
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches how random numbers are generated in computer programming languages and the limitations of pseudo-random number generation. It explains how pseudo random number generators work and how they can be controlled using a seed value. The video also discusses the potential security issues of pseudo-random number generation, especially in cryptography.

Key Takeaways
  1. Import the random module in Python
  2. Use the rand function to generate a random number in a specified range
  3. Use the seed function to generate the same sequence of random numbers
  4. Run the code with a manually set seed value to produce a specific sequence of numbers
  5. Run the code with the system time as the seed value to produce a different sequence of numbers each time the code is run
💡 Pseudo random number generators can lead to security issues, especially in cryptography, and truly random numbers can be generated using external sources like radioactive decay, mouse movement, and keyboard timing.

Related Reads

📰
You Have Been Buying Graphics Cards From a Gaming Company for Twenty Years.
NVIDIA's revenue is now dominated by data centers, not gaming, with 90% of income coming from this sector
Medium · Machine Learning
📰
Neil Rimer thinks the AI money is coming back out
Neil Rimer predicts AI-generated wealth in Silicon Valley will be redistributed, and you can apply this insight to inform your investment and business strategies
TechCrunch AI
📰
Technology is moving forward. But are we?
Explore how technology advancement impacts human progress and what it means to move forward as a society
Medium · AI
📰
Databricks hits $188B valuation, extending its run as AI’s favorite second act
Databricks hits $188B valuation as it transforms into an AI company, focusing on open weight AI models for coding
TechCrunch AI

Chapters (6)

| Why Random Numbers Aren't Random
1:58 | Random Module Documentation
2:23 | Generating Random Numbers in Python
3:30 | Random Seeding
5:45 | Using System Time As A Seed
7:11 | How To Generate Truly Random Numbers
Up next
Big Tech is spending MORE than the US Military?!
AmplifyME
Watch →