Why it worked
The post effectively breaks down a complex technical topic into easily digestible slides, using clear explanations and a practical code example. The hook is engaging, and the content provides valuable information without a hard sell, appealing to a broad audience interested in programming and computer science.
Summary
This post explains the two types of random numbers: true random numbers and pseudo-random numbers (PRNs). It details how PRNs are generated using a seed and algorithm, demonstrating with a Python code example that resetting the seed produces the same sequence. The post concludes by explaining when each type of random number is appropriate, with PRNs suitable for general use like video games and true random numbers necessary for critical applications like encryption.
On-screen text
iT
How random
numbers are
generated
SWIPE
iT
You probably already
know how to generate
random numbers in
programming, be it in
Python, Java, C, etc.
iT
But have you ever
wondered how a
computer actually
generates them?
iT
First, you should know
that random numbers
can be of 2 types...
True random numbers
True 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.
iT
When using a function
that generates random
numbers, you are in
fact generating PRNs.
PRNGs produce a long
sequence of numbers
that eventually repeat.
iT
The only random part
of PRNs is the starting
point of the sequence.
If you start with the
same seed, you'll get
the same sequence.
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 setting the seed value
back to 3, the exact same
numbers got generated again
iT
PRNs are fine to use
when you don't really
need numbers that are
completely random,
like in video games.
iT
But in some cases, it's
crucial to use numbers
an attacker can't guess,
like in encryption.
That's when true random
numbers and specialized
hardware generators
come into play.
iT
Do you want a post about
true random numbers?