How Long Would It Take a Monkey to Write Hamlet?

The Infinite Monkey Theorem and What It Teaches Us About Randomness, Infinity, and AI

Imagine a monkey, randomly typing on a keyboard, key after key, day after day. Could it ever write Hamlet? Or Don Quixote? Or maybe even the next bestselling novel?

As absurd as it sounds, this idea is at the heart of a fascinating mathematical concept known as the infinite monkey theorem. While we may never actually find an immortal monkey willing to take on this task, the theory opens the door to exploring ideas like randomness, probability, and the limits of computation.


🎲 The Infinite Monkey Theorem: A Thought Experiment

The theorem is often summarized like this:

“Given infinite time, a monkey hitting keys at random on a typewriter would almost surely type out the complete works of Shakespeare.”

This is not magic — it’s probability. The core idea comes from a branch of mathematics dealing with infinite processes, and particularly from the Second Borel–Cantelli Lemma. The lemma tells us that if an event has a non-zero probability and we repeat the experiment infinitely, that event will not only occur, but will occur infinitely often.

So even if the chances of typing “To be, or not to be” randomly are incredibly low, they’re not zero. Given infinite attempts, it would eventually happen.


🧮 How Low Are the Odds?

Let’s consider a simple example: what’s the probability of a monkey typing the word “hola” at random on a Spanish keyboard (which includes 27 characters — 26 letters + space)?

The probability is:
(1/27)^4 = ~0.0000019

That’s already a very small number — and “hola” is only four characters long. Now imagine trying to randomly generate a 30,000-word play like Hamlet.

But here’s the catch: when you repeat such an experiment an infinite number of times, the probability that the correct sequence never appears approaches zero. So, theoretically, the monkey will not only write Hamlet — it will do so over and over again, infinitely.


⏳ But How Long Would It Actually Take?

Let’s get practical. If we wanted to estimate how long it would take for monkeys — real or virtual — to type Hamlet, we’d run into a wall. A recent mathematical study estimates that even if every monkey alive today typed non-stop, they would likely only manage to recreate a few coherent words before the heat death of the universe.

So, yes, Hamlet is possible in theory, but wildly implausible in reality.


💻 Can AI Replace the Monkey?

With the rise of large language models like ChatGPT, some might wonder: what if we replaced the monkey with artificial intelligence? Could a model like GPT-4 or DeepSeek eventually recreate Don Quixote just by running long enough?

The answer is more nuanced than you might think.

Unlike our hypothetical monkey, language models don’t generate text randomly. Instead, they rely on statistical probability — predicting what word is most likely to come next based on context. They are trained on vast datasets and optimize for coherence and fluency, not randomness.

So, yes, a model might produce fragments of Don Quixote, especially if it’s part of its training data. But reproducing the entire novel, word for word, in the original language and style, is extremely unlikely — and also goes against the ethical and technical design of these models. They are specifically built not to reproduce large copyrighted or public domain texts exactly.


🔢 True Randomness vs Pseudorandomness

There’s another fascinating twist: when we simulate the monkey theorem online (there are websites where you can try this), the “random typing” is not truly random. These systems use pseudorandom number generators — algorithms that mimic randomness.

Although they seem unpredictable, pseudorandom numbers are deterministic. If you know the initial state (called the seed), you can predict every “random” number the system will produce.

Still, for most purposes, these pseudorandom outputs are indistinguishable from real randomness. They’re good enough to simulate the monkey, but not quite the real deal.


🧠 Why It Matters: Beyond Monkeys and Shakespeare

So, why do mathematicians and computer scientists care about a thought experiment involving monkeys and typewriters?

Because it forces us to think about:

  • The nature of randomness
  • The power (and limits) of infinity
  • The role of probability in computing and AI
  • The difference between theoretical possibility and practical reality

It also invites us to consider the nature of creativity, intention, and meaning. After all, even if a monkey writes Hamlet, does it understand what it has written? And if an AI model writes something poetic, is it art — or just statistics?


☕ From Coffee to Theorems

This reflection comes from Coffee and Theorems, a space where mathematicians explore ideas that connect their work with society, art, and philosophy. As Hungarian mathematician Alfréd Rényi once said:

“A mathematician is a device for turning coffee into theorems.”

It seems that today, even monkeys — and AIs — are part of the story.