What is Random State? And Why is it Always 42?

An inquiry into what the number 42 has to do with machine learning and the universe

Graham Sahagian
4 min readNov 30, 2020

If you’re like me and have used or been interested in using artificial intelligence or machine learning to solve various real-world problems, then you have undoubtedly run into the mystical Random State variable and wondered: why is it always 42? Well, turns out the answer is far less mundane than the explanation you typically get on StackOverflow threads about esoteric variable settings.

If you’re just here for the answer to the titular question, then here it is: the number doesn’t really matter; it’s an arbitrary number fed into the program to dictate that the program should use a specific seed of randomness (42). This is so the same set of pseudo-random numbers is generated each time we run the program. This answer isn’t quite satisfactory though as it leads us to another larger question: If it doesn’t matter then why do people always choose 42?

Turns out the answer to this question is equal parts science and fiction. The number 42 is sort of an ongoing inside joke in the scientific and science fiction community and is derived from the legendary Hitchhiker’s Guide to the Galaxy by Douglas Adams wherein an enormous supercomputer named Deep Thought calculates the “Answer to the Ultimate Question of Life…” over the period of 7.5 million years. This was originally intended to be entirely a joke but since its publication in 1979, science fiction junkies and nerds alike have attempted to impute meaning to the number and somewhat verify its claim of answering the “Ultimate Question of Life, the Universe and Everything”. You can check out the many pop culture references to the number 42 on its dedicated Wikipedia page.

The ironic part is that Douglas Adams himself undermines everyone’s obsession with the number:

“It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. Binary representations, base 13, Tibetan monks are all complete nonsense. I sat on my desk, stared in to the garden and thought 42 will do. I typed it out. End of story.”

Despite Adams’ attempts to quell the world’s feverish fascination with the number, people don’t hesitate to search for and point out the occurrences of the number 42. To name a few:

  • The Titanic was traveling at a speed of 42 km/hour when it collided with the iceberg
  • On page 42 of Harry Potter and the Philosopher’s Stone, Harry finds out he is a wizard
  • In the Book of Revelation, it is prophesied that the beast will hold dominion over the earth for 42 months.
  • 42 is the angle rounded to whole degrees for which a rainbow appears (the critical angle).
  • Lewis Carrol, a mathematician and writer made use of the number several times in his works. For example, rule Forty-two in Alice’s Adventures in Wonderland (“All persons more than a mile high to leave the court”).
  • Alice’s attempts at multiplication (chapter two of Alice in Wonderland) work if one uses base 18 to write the first answer, and increases the base by threes to 21, 24, etc. (the answers working up to 4 × 12 = “19” in base 39), but “breaks” precisely when one attempts the answer to 4 × 13 in base 42, leading Alice to declare “oh dear! I shall never get to twenty at that rate!”

So, why do we set Random State to 42 when running Machine Learning algorithms?

Because, it is simply the answer to life, the universe and everything.

…Or if you’re a bit less romantic about it, it’s because geeks and nerds alike really enjoy science fiction and one of the most famous science fiction books ever written heavily associates the number 42 with advanced machine learning and artificial intelligence.

I, personally, love running into little easter eggs like this in my life, and get a kick out of the fact that people around the world unquestioningly set 42 as their random state variable, unaware of its hilarious history and pop culture relevance.

If you liked this article and would like to read more stuff like this in the future, consider giving me a follow on Medium. Thanks for reading.

--

--

Graham Sahagian

Learning and teaching how to do useful stuff, usually with code