On-screen text
IT
How Random Really Works
SWIPE >>>
You probably already know how to generate a random number in programming, be it in Python, Java, C, etc.
However, have you ever wondered how a computer generates a random number?
First, you should know that random numbers can be of 2 types...
Truly random numbers
Truly random numbers are mainly generated by considering some real world physical process, such as the mouse movement. That makes the numbers unpredictable, but they are also slower to generate.
--------------------------------------------------
Pseudo-random numbers
On the other hand, PRNs are generated only within the computer by using an initial value, called seed, and an algorithm. That means the numbers get generated quickly, but they are also deterministic.
When using a function that generates random numbers, you are in fact generating PRNs.
PRNGs produce a long sequence of numbers that keep repeating.
The only random part of PRNs is where in the sequence you start at.
That means if you start at the same place you'll get the same numbers. Let's see an example...
import random
random.seed(3)
print(random.randint(1, 100))
print(random.randint(1, 100))
print(random.randint(1, 100))
# Setting the seed back to 3
random.seed(3)
print(random.randint(1, 100))
print(random.randint(1, 100))
print(random.randint(1, 100))
Output:
31
76
70
31
76
70
After we set the seed value back to 3, the exact same numbers got generated again
PRNs are fine to use when you don't really need numbers that are completely random, like in video games.
But in some cases, it's critically important to have numbers that an attacker can't guess, like in encryption.
That's when TRNs and HW devices to generate them come into play.
Was this post useful to you? Check out more on my profile!
IT Challenges
Coding content
OUR E-BOOK IS OUT NOW!
9 GREAT JAVA PROJECT IDEAS
Easy
Mid
Pro
4 PILLARS OF OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
Escape From The Tutorial Hell
9 AREAS WHERE CODING IS POPULARLY USED
AI
Machine Learning
Web Development
Mobile Apps
Game Development
Cybersecurity
Data Science
DevOps
Blockchain
7 APIs You Should Try
DYNAMIC MEMORY ALLOCATION IN C
Six Google Search Tips
9 GOOD RESOURCES TO LEARN CODING
U
U
U
U
U
U
U
U
U
Switch-case In Python
THE EISENHOWER DECISION MATRIX
SWIPE >>>
@ITCHALLENGES
SWIPE >>>