Post

There is nothing Random inside a computer

There is nothing Random inside a computer

{% raw %}

Have you ever wondered how your music app shuffles thousands of songs, or how a video game generates a brand new world every time you hit “New Game”? It feels like digital magic. We call it “random,” but in the world of silicon and circuits, true randomness is actually quite hard to find.

In fact, your computer isn’t “guessing” at all. It’s following a recipe.

The Secret Recipe: The PRNG

Computers are built to be predictable. If you ask a calculator what 2+2 is, you expect 4 every single time. If it suddenly said 5 just to be “spontaneous,” you’d throw it in the trash.

Because computers are designed to follow instructions, they use a Pseudorandom Number Generator (PRNG).

Python Reference: https://docs.python.org/3/library/random.html

Think of a PRNG as a massive book of numbers hidden in the computer’s memory. This book contains millions of digits that look like they have no pattern, but the order is actually fixed. When you ask for a random number, the computer simply opens the book to a specific page and reads the next line.

The “Seed” – Choosing the Page

If the “book” of numbers is always the same, how do we make sure we don’t get the same “random” results every time we restart the computer?

That’s where the Seed comes in.

The seed is the starting point. If you give the generator the same seed, it will always start on the same page and read the same numbers in the same order.

Why would we want that?

Imagine you are a game developer. You’ve created a beautiful “random” forest. If a player finds a bug in that forest, you need to see exactly what they saw. By using the same Seed, you can recreate that exact “random” forest for debugging.

What happens if you don’t set a seed?

If you don’t tell Python (or any language) which seed to use, it doesn’t just panic. It goes looking for a “natural” seed.

Most modern systems look at the System Clock. It takes the current time down to the nanosecond (e.g., 1707314354.92834). Because time never moves backward, the “starting page” for your random numbers will be different every single time you run the script.

To you, it looks like pure chaos. To the computer, it’s just math based on what time it was when you clicked “Run.”

This example proves that “random” is actually deterministic. If you run this code ten times, the output will be identical every single time.

Python
import random
# Setting a fixed seed
random.seed(42)
print("First Number:", random.random())
print("Second Number:", random.random())

On the default case, you can’t easily see the “seed” number once it’s set, but you can see the State the internal numbers the computer is currently crunching.

Python
import random
random.seed(10)
# Capture the internal state of the generator
internal_state = random.getstate()
# The state is a massive tuple of data
print(f"Internal data snippet: {internal_state[1][:5]}")

You can use the below table for future reference,

ScenarioCodeResultBest For
Fixed Seedrandom.seed(7)Always the same sequenceDebugging, scientific research, sharing game maps.
Default (No Seed)random.random()Different every timeGeneral apps, shuffling music, casual games.
Cryptographicsecrets.token_hex()Mathematically unpredictablePasswords, bank tokens, security keys.

{% endraw %}

This post is licensed under CC BY 4.0 by the author.