PYTHON CHARMERS CLUB

The story behind ‘random.seed(42)’ in machine learning

Answer to the great question of “Life, the universe, and everything”!

Nazia Nafis
Geek Culture

--

📷 — Ihor Malytskyi

You must have come across random.seed(42) some time or the other in data science/machine learning. So what does it mean?

First, let’s review some basics:

What is random() ?

random() is a function that is used to generate pseudo-random numbers in Python.

Computer algorithms can only produce seemingly random (or pseudo-random numbers), although some naturally occurring phenomena could be incorporated to create randomness artificially.

Sherlock shouting “That’s so RANDOM!”

For the random() function, Python uses Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937–1. The Mersenne Twister is also one of the most extensively tested random number generators in existence. Being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.

What is random.seed() ?

random.seed() function initializes the random number generator with the given value.

The random.seed(a=None, version=2) function takes the following two arguments:

  • If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time. And if a is an int, it is used directly.
  • With version=2 (which has become the default since Python 3.2), a str, bytes, or bytearray object gets converted to an int and all of its bits are used. In version 1, the hash() of a was being used instead.

The seed is given an integer value to ensure that the results of pseudo-random generation are reproducible. By re-using a seed value, the same sequence should be reproducible from run to run as long as multiple threads are not running. Reproducibility is a very important concept that ensures that anyone who re-runs the code gets the exact same outputs.

Input

import random
for i in range(5):
random.seed(1)
print(random.randint(1, 1000))

Output

138
138
138
138
138

What is the significance of random.seed(42) ?

It’s a pop-culture reference!

In Douglas Adams’s popular 1979 science-fiction novel The Hitchhiker’s Guide to the Galaxy, towards the end of the book, the supercomputer Deep Thought reveals that the answer to the great question of “life, the universe and everything” is 42.

[Seven and a half million years later…. Fook and Lunkwill are long gone, but their descendants continue what they started]

“All right,” said Deep Thought. “The Answer to the Great Question…”
“Yes..!”
“Of Life, the Universe and Everything…” said Deep Thought.
“Yes…!”
“Is…” said Deep Thought, and paused.
“Yes…!”
“Is…”
“Yes…!!!…?”
“Forty-two,” said Deep Thought, with infinite majesty and calm.”

―Douglas Adams, The Hitchhiker’s Guide to the Galaxy

📷 — screen grab from the movie

Adams’ choice of the number 42 has become a fixture of geek culture. People find humor in it, and some, out of reverence for the classic sci-fi literature use 42 in various places.

There have been many theories from fans in an attempt to explain why the number 42 was chosen.

  • Some propose that it was chosen because 42 is 101010 in binary code, others have pointed out that light refracts through a water surface by 42 degrees to create a rainbow.
  • Others have commented that light requires 10⁻⁴² seconds to cross the diameter of a proton.
  • Yet others say that 42 is Adams’ tribute to the indefatigable paperback book, and is the average number of lines on an average page of an average paperback.
  • Another common theory is that 42 refers to the number of laws in cricket, a recurring theme of the books¹.

However, Douglas Adams himself revealed the reason why he chose 42 in this message. “It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. I sat at my desk, stared into the garden and thought ‘42 will do!’ ”

Check out the The Hitchhiker’s Guide to the Galaxy if you haven’t already, and keep practicing Python programming! 🐍

I hope this article was of use to you. You can connect with me on LinkedIn, or follow my writings here.

Until next time! (∗ ・‿・)ノ゛

--

--