GCSE
GCSE (Foundation)GCSE (Higher)
A-level
IB
IB AA (Standard Level)IB AA (Higher Level)
1-1 Tutoring
← All articles

How long would a monkey take to type Hamlet?

58,149,737,003,040,059,690,390,169 to one. Those are the odds against a monkey, hammering 18 random keys, typing the phrase "to be or not to be" — about 5.8×10255.8 \times 10^{25} to one. The idea that a monkey typing at random would eventually produce the complete works of Shakespeare goes back to the mathematician Émile Borel in 1913 [1], and it is quoted so often that it deserves a proper check. Everything we need is one rule of probability: the probabilities of independent events multiply.

The odds on six words

We must first state our model. Our monkey types on a simplified 27-key typewriter: the 26 letters plus a space bar — no capitals, no punctuation. Every keystroke is equally likely (probability 127\frac{1}{27} per key) and independent of every other keystroke.

Independence is what makes the arithmetic easy. If two events are independent, the probability that both happen is the product of their probabilities. The chance the first keystroke is "t" is 127\frac{1}{27}; the chance the second is "o" is also 127\frac{1}{27}; the chance of "t" then "o" is 127×127=1729\frac{1}{27} \times \frac{1}{27} = \frac{1}{729}.

Each keystroke has probability 1/27, and independent probabilities multiply keystroke by keystroke

The phrase "to be or not to be" contains 1818 characters, spaces included. The probability of typing it in 1818 given keystrokes is therefore:

P=(127)18=1271815.8×1025P = \left(\frac{1}{27}\right)^{18} = \frac{1}{27^{18}} \approx \frac{1}{5.8 \times 10^{25}}

A failed attempt tells the monkey nothing — it just keeps typing — and on average it will need about 271827^{18} keystrokes before the phrase first appears. At a steady one keystroke per second:

2718 seconds5.8×1025 s1.8×1018 years27^{18} \text{ seconds} \approx 5.8 \times 10^{25} \text{ s} \approx 1.8 \times 10^{18} \text{ years}

That is roughly 130 million times the age of the universe — a figure of 13.813.8 billion years that we derive in another article — for six words. Not the play. Not the soliloquy. Six words.

Every atom a monkey

Perhaps our monkey just needs colleagues. Let us hire the most absurd workforce imaginable: one monkey for every atom in the observable universe — about 108010^{80} of them — each typing one key per second since the Big Bang, 4.4×10174.4 \times 10^{17} seconds ago. The total output is:

1080×4.4×10174.4×1097 keystrokes10^{80} \times 4.4 \times 10^{17} \approx 4.4 \times 10^{97} \text{ keystrokes}

How long a specific passage could this cosmic typing pool be expected to produce? We need 27n4.4×109727^{n} \approx 4.4 \times 10^{97}, and solving gives n68n \approx 68 characters. Sixty-eight characters — not even the second line of the soliloquy.

Now compare that to the actual assignment:

  1. One page of Hamlet is roughly 1,8001{,}800 characters, and 27180010257627^{1800} \approx 10^{2576}. Our universe of monkeys falls short by nearly 2,5002{,}500 orders of magnitude.

  2. The whole play contains roughly 130,000130{,}000 letters. Ignoring spaces entirely (a 26-key model, to be generous), the odds against typing it in one go are 261300004×10183,94626^{130000} \approx 4 \times 10^{183{,}946} to one — a number whose digit count is itself a six-figure number.

Infinity does all the work

So how long would a monkey take to type Hamlet? On any physical timescale: forever. The infinite monkey theorem is mathematically true — with genuinely infinite time, the probability of typing Hamlet really does approach 11 — but the word "infinite" is doing all of the work, because the required time dwarfs the age of the universe by hundreds of thousands of orders of magnitude. There has even been an experimental test: in 2003, researchers at the University of Plymouth left a computer keyboard with six Sulawesi crested macaques at Paignton Zoo for a month. The monkeys produced five pages consisting mostly of the letter S, then used the keyboard as a toilet [2]. Real monkeys, it turns out, are not even random.


References:

[1] Émile Borel, "Mécanique statistique et irréversibilité", Journal de Physique Théorique et Appliquée, 5e série, tome 3 (1913).

[2] Notes Towards the Complete Works of Shakespeare — the Vivaria Project, University of Plymouth / Paignton Zoo (2003).

Note: Our model assumes uniformly random, independent keystrokes on a 27-key typewriter. Real monkeys type nothing like randomly — which, as the Paignton Zoo experiment showed, only makes matters worse.