The Mathematical Case for Monkeys Producing Shakespeare—Eventually
An infinite number of random events can produce just about anything if you have quintillions of years to wait
In one of the most bizarre research experiments in the history of mathematics, researchers at the University of Plymouth in England gave six Celebes crested macaques at the nearby Paignton Zoo a keyboard. From May 1 to June 22, 2002, the animals let off steam by banging at the keys. The letters they typed were transmitted to the Internet. The scientists’ aim was to test the “infinite monkey theorem”: the idea that a monkey typing randomly on a keyboard will, after an infinite amount of time, produce every conceivable text—including the complete works of Shakespeare.
But—surprise, surprise—the primates’ poetry fell short. After more than seven weeks, the macaques produced only one five-page document, consisting almost entirely of the letter “S.” The researchers nonetheless released the result as a book.
In defense of Elmo, Gum, Heather, Holly, Mistletoe and Rowan—the six macaques who participated in this experiment—they did not have an infinite amount of time for their work. Still, the result was sobering. It seems highly questionable that these monkeys would have produced Hamlet or the “Scottish play.”
On supporting science journalism
If you’re enjoying this article, consider supporting our award-winning journalism by subscribing. By purchasing a subscription you are helping to ensure the future of impactful stories about the discoveries and ideas shaping our world today.
Although the study in no way disproved the infinite monkey theorem, it showed that monkeys are not necessarily ideal candidates for generating random content in the way the theorem’s creator had assumed. The infinite monkey theorem owes its name to mathematician Émile Borel, who used the animals metaphorically to illustrate his theory of probability in 1913. The ideas behind the theorem are much older, however. In antiquity, Roman philosopher and politician Marcus Tullius Cicero wrote that one might “believe that if a great quantity of the one-and-twenty letters, composed either of gold or any other matter, were thrown upon the ground, they would fall into such order as legibly to form [the epic poem] the Annals of Ennius. [But] I doubt whether fortune could make [even] a single verse of them.”
As the Paignton Zoo macaques illustrate, mathematicians have taken creative steps to explore this possibility. Today research suggests that Cicero was incorrect: poetry can emerge out of this exercise in randomness, provided you can wait a very long time.
The Probability of a Particular Word Appearing
Let’s check this idea with a simple example. How likely is it that you will get the word “banana” completely by chance when you randomly press a series of letter keys for a given number of times? (In this scenario, you don’t press any numbers or special characters.) If you press six keys consecutively, each chosen at random, the probability is (1⁄52)6 = 1⁄19770609664, which is about five billionths of 1 percent. In other words, the probability of not typing banana is 1 – (1⁄52)6, which is almost a probability of one. Overall, it is therefore very unlikely to get the word banana if you press six keys at random. But what if you hit the keys for longer?
If you randomly press a sequence of seven keys, there are two consecutive segments of six letters that can form the desired word. With eight keystrokes, there are three strings of six letters, and so on. If you hit the keyboard n times at random, the probability that the word banana will not appear anywhere in the string is (1 – (1⁄52)6)n–5. For increasing values of n, the odds that “banana” will not appear gradually decrease:
Thus, the odds that “banana” will appear gradually improve: If you randomly press a key on a keyboard 10 billion times, the probability of the word appearing somewhere is suddenly around 40 percent. The larger n becomes, the closer the probability of finding the word banana approaches one. Of course, the result also applies to other sequences of letters, words or even entire sentences and books. Therefore, from a mathematical point of view, Cicero was wrong.
The Infinite Monkey Theorem in Practice
In 2024 data analyst Ergon Cugler de Moraes Silva of the University of São Paulo in Brazil wanted to find out how long it would take, on average, to actually obtain a work by Shakespeare by chance. Instead of monkeys, he programmed a character generator. As described in a preprint paper, his technique was designed to rapidly spit out several hundred pseudorandom spaces, punctuation and letters (in both upper and lower case) each second until aa famous sentence from Hamlet appeared: “To be, or not to be, that is the Question” (including the spaces).
Cugler proceeded in several steps. First, he examined how long it takes on average to find “T” in isolation. He repeated that procedure 10 times and then recorded the average time and number of characters required to generate the desired output. Then he repeated this procedure to determine how long it would take to randomly generate “To” and then did so again for “To” followed by a space, and so forth.
As the table below shows, his program had to randomly generate around 60 characters to produce “T” and generated an average of 345,380,940 characters for “To be.” Those two words also took his program about 1,100 seconds (a little more than 18 minutes).
At this point Cugler hit a roadblock. Given the increase in time required for correctly generating the next character in the sentence, he realized the task could potentially require his program to keep spitting out characters until the end of humanity (assuming the computers involved would continue to function for that long). So in a second step, Cugler designed a program that used the previously determined data to extrapolate the number of characters and computing time required to generate the entire sentence.
Cugler’s calculation showed that it would take an extreme amount of patience for “To be, or not to be, that is the Question” to appear: about 2.68 x 1069 characters would have to be generated, which would take about 2.95 x 1066 seconds, or 9.35 x 1058 years.
Since our universe is estimated to be 13.8 billion years old, we would have to wait nearly 7 x 1048 times as long as the time that has passed between the big bang and today. And all this just to produce a single sentence from Hamlet by chance. In this respect, Cicero was right: it is very unlikely that chance will produce even a single readable verse of a poem—or any other text—after a finite amount of time.
This article originally appeared in Spektrum der Wissenschaft and was reproduced with permission.