The infinite monkey theorem explained
Give a monkey a typewriter and infinite time, and it will type Shakespeare. It sounds like a joke, and it is true, but only because infinity is so much larger than anything real. Here are the theory, the numbers behind a single word, what happens when the time is merely astronomical, and what a zoo's monkeys really typed.
The theorem
The infinite monkey theorem says that a monkey hitting keys at random on a typewriter for an infinite amount of time will almost surely type any given text, such as the complete works of William Shakespeare. Almost surely is a precise phrase in probability: the chance that it never happens is exactly zero, even though it is not impossible. The argument is short. Split the endless stream of keys into blocks as long as the text. Each block has the same tiny chance p of being the text, independently of the others, so the chance that none of the first n blocks is the text is \((1 - p)^{n}\), which shrinks to zero as n grows. Given forever, the monkey types Hamlet, and then types it again, infinitely often.
Borel's monkeys and Eddington's army
The monkeys first appeared in 1913, in an essay by the French mathematician Émile Borel on statistical mechanics. Borel imagined a million monkeys typing ten hours a day to describe events that are possible but so improbable that they never happen in practice, like all the air in a room gathering in one corner. In 1928 the astronomer Arthur Eddington wrote that an army of monkeys strumming on typewriters might write all the books in the British Museum. The image stuck, and it has turned up since in novels, in The Simpsons and The Hitchhiker's Guide to the Galaxy, and in jokes about the internet.
The arithmetic of a single word
Take a keyboard of 30 keys: the 26 letters, a space, a full stop, a comma and an apostrophe. Each key the monkey hits is right with a chance of 1 in 30. A word of L letters is right at any one place with a chance of 1 in 30ᴸ, because the choices multiply. Cat has odds of 1 in 27,000, monkey 1 in 729 million, bananas 1 in 21.9 billion. Each letter more makes the odds 30 times longer, so the odds grow exponentially with the length of the text. A sentence of 40 characters has odds of about 1 in \(10^{59}\), and a page of 2,000 characters about 1 in \(10^{2954}\).
How long the wait is
Odds of 1 in 21.9 billion mean that a monkey typing a key a second needs about 21.9 billion seconds, some 694 years, on average before bananas first appears. A chimp lives about 35 years, roughly 1.1 billion keystrokes, so its chance of typing bananas in its whole life is \(1 - \e^{- 1.1 / 21.9}\), about 5 %. A surprise hides here: not every word of the same length takes equally long. A word that overlaps itself, like aaa or abab, makes its finds come in clumps, and its first find comes later on average. The expected wait is the sum of \(1 / P\)(prefix) over every beginning of the word that is also an ending of it, so on 26 keys aaa takes
\[26 + 26^{2} + 26^{3} = 18{,}278\]
keys on average, while abc takes only \(26^{3} = 17{,}576\). The same idea explains Penney's game with coin tosses.
The finite monkeys theorem
Real time is not infinite. In 2024 Stephen Woodcock and Jay Falletta of the University of Technology Sydney asked what the theorem means with real numbers: the roughly 200,000 chimpanzees alive today, each typing a key a second on 30 keys, until the heat death of the universe, about \(10^{100}\) years from now. That comes to about \(6 \times 10^{112}\) keystrokes. A single chimp has about a 5 % chance of typing bananas in its life. All of them together, until the end of time, would type any particular text of up to about 76 characters, a line or two of Hamlet. But Shakespeare's complete works run to some 5 million characters, with odds of 1 in 10 to the power of more than 7 million. The universe dies first, and the chance that the chimps finish the works is so close to zero that it makes no difference.
What real monkeys typed
In 2003 students and staff from the University of Plymouth put a computer keyboard into the enclosure of six Sulawesi crested macaques at Paignton Zoo in Devon for a month. The monkeys produced five pages of text, consisting mostly of the letter S, with some A, J, L and M towards the end. The lead male also bashed the keyboard with a stone, and others used it as a toilet. Real monkeys are not random typists: they have favourite keys and habits. A typist that hits S half the time types sssss very quickly, but almost every other text far more slowly than a fair random typist would.
Chance is not selection
The theorem is sometimes used to argue that complex things cannot arise by chance, and the biologist Richard Dawkins answered with his weasel program in The Blind Watchmaker (1986). Typing METHINKS IT IS LIKE A WEASEL, 28 characters on 27 keys, at random would take about \(10^{40}\) tries. But if each generation keeps the copies closest to the target and makes small random changes to them, the phrase appears in a few dozen generations. Evolution works by such cumulative selection, not by single leaps of pure chance. The writer Jorge Luis Borges turned the monkeys' output into a library in The Library of Babel (1941): every possible book of 410 pages, almost all of it gibberish, with every true book lost somewhere among them.
Explore it
Open Infinite Monkey Theorem to watch a monkey hit random keys on a typewriter, with every stretch of the paper that matches your text lit up and the text as a row of tiles above. Read the odds at one place, the expected wait and the chance in the time you allow, from an hour to the end of the universe, for one monkey, every chimp on Earth or one for every person. The chart shows the chance of each beginning of the text against time, so you can see every extra letter push the curve 30 times further, and you can type your own text or let the zoo's monkeys try.
Frequently asked questions
Is the infinite monkey theorem true?
Yes, as a statement about probability: with infinitely many random keys, any given text appears with probability 1. It says nothing about real monkeys or real time. With any finite time, even until the end of the universe, a long text such as a play or a book is practically certain never to appear.
What are the odds of a monkey typing a word?
On a keyboard of n keys, a word of L letters has odds of 1 in nᴸ at any one place. On 30 keys, cat is 1 in 27,000, monkey 1 in 729 million and bananas 1 in about 22 billion. At a key a second, bananas takes about 694 years on average.
Could all the monkeys in the world type Shakespeare?
No. Even the 200,000 chimpanzees on Earth typing a key a second until the universe ends, about 10¹⁰⁰ years, would type about 6 × 10¹¹² keys. That is enough for any text of about 76 characters, but Shakespeare's complete works have about 5 million characters and odds of 1 in 10 to the power of more than 7 million.
What did the Paignton Zoo monkeys type?
In 2003, six crested macaques given a computer keyboard for a month at Paignton Zoo typed five pages, mostly the letter S. They also hit the keyboard with a stone. The experiment showed that real animals are far from random typists.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open Infinite Monkey Theorem