Monkeys on Typewriters
Friday, January 8, 2010
Dilbert by Scott Adams
Yes, I did start my last post with a comic. Yes, that is two posts with comics in a row. No, this is not a trend.
The Infinite Monkey Theorem
A monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare.
“Almost surely” is a mathematical term meaning the event has a probability of 100% — what we normal people call “will happen”. Therefore, given an infinite amount of time, a monkey on a typewriter will, if also given infinite resources, produce any given text while typing at random.
But how much time would we expect to have to wait for a work to be produced?
Setting Up Variables
Firstly, as this is based on randomness and thus probability, it will take an infinite amount of time for anything to have a percentage chance of 100%. Instead, we can look for how long it will take for the event to be “likely” — a percentage chance of greater than 50%.
Then, we need to define our monkey. For this experiment, we have Bob. Say “Hi” to Bob.

Bob can type at the same typing speed of an average human — 60 words per minute — which is 5 strokes of a key in a single second. Except Bob thrashes around randomly, producing challenging prose as “QHFMBVVAENOIGIEFGAAG”. Eventually, we can expect some of his text to form the word “MONKEY”. Also Bob has been genetically engineered to be able to type 24 hours a day, without requiring any rest or food, and without losing any productivity.
The Keyboard and Probabilities
We also have our keyboard. For simplicity, our keyboard has 26 letters (“A” through “Z”). We are being very forgiving to our monkeys — we’re ignoring all capitalization, punctuation, and spacing.
The probability of hitting any given letter is 1/26, or 3.846%. Thus, the probability of any monkey keystroke being “M” is 3.846%, and the chance of the first two letters of monkey — “MO” — occurring together is (1/26)(1/26), which is 1/676, or 0.148%.
As we noticed, the chance of typing any length of letters exactly as we specify is 1/26 for each letter. When we look for combined probabilities, we multiply all the chances together — (1/26) times itself for each letter of our desired word, or (1/26)X, where X is the number of letters in a row to be typed correctly. With each additional letter, the chance goes down dramatically.
Therefore, the probability of typing “MONKEY” in any given six consecutive keystrokes is (1/26)6, which evaluates to 1/308915776, or 0.000000324%. That’s roughly the same chance as flipping 21 quarters and getting all heads.
Bob, typing away randomly at 60wpm, will have a 50% chance of typing “M” within 2.6 seconds. “MO” will reach a 50% chance of occurring within 1 minute, 7.6 seconds. If you want a 50% chance of “MONKEY”, you’ll have to wait for 357 days, 12 hours, 59 minutes, and 37.6 seconds.
Typing Shakespeare
“The Complete Works of Shakespeare” includes 38 plays, 154 sonnets, two long narrative poems, and several other poems. It’s unknown how many letters are in all of them (You want to count them?), but there are roughly 130,000 letters in “Hamlet”, one of Shakespeare’s plays.
Hamlet starts out with the words “ACT I, SCENE I. Elsinore. A platform before the castle.” In Monkey typing (ignoring everything but 26 letters), “ACT I, SCENE I” becomes “ACTISCENEI”. The probability of any given ten consecutive keystrokes being “ACTISCENEI” is (1/26)10, which is 1/141167095653376, or 0.00000000000071%.
Using some common examples, that’s roughly the same chance as…
- Flipping 47 coins and getting all heads. Or…
- Rolling 18 standard dice and getting all “6″s. Or…
- Pulling eight “Aces of Spades” by shuffling a Jokerless standard playing deck, pulling out a card at random, and then put it back. Or…
- Generating a random number between 1 and 150 trillion and getting “436,219″.
Poor Bob will have to spend 447637 years, 337 days, 4 hours, 35 minutes, and 37.6 seconds before he has a 50% chance of having typed “ACTISCENEI”.
And it will be in some random gibberish, like “XBAMGPMLACTISCENEIBBTRSWMQ”. If you want it in Hamlet, you’ll have to wait longer.
Upping the Ante
Lets up the ante. Let’s get every single person in the world to create their own “Bob”, and put all those “Bob”s to work on our special typewriters. Lets automatically combine all their work into one manuscript, and search that for the words we’re looking for. Lets also spend some time training these monkeys so they can type at 20 letters per second, yet they still thrash around at random, with an equal chance of hitting any given letter.
The population of the world is currently 6.692 billion people, meaning we’re looking at 6.692 billion “Bob”s. If they all type at 20 letters per second, we’re covering 8.03 trillion letters in any given minute. So how long will we have to wait for the collective Bobs to type out “ACTISCENEI”?
We’ll have a 50% chance of “ACTISCENEI” within 8 minutes, 47.4 seconds.
The Full First Line of Hamlet
The full first line is “ACT I, SCENE I. Elsinore. A platform before the castle.” In Monkey script, we’re looking for “ACTISCENEIELSINOREAPLATFORMBEFORETHECASTLE”, which is 42 letters. The chance of typing all 42 letters correctly on the first attempt is:
…which is very small. If we put our population of fast-typing Bobs to work, it will take:
keystrokes
…for a 50% chance, which means the Bob population will take:
seconds.
That’s over 30,000,000,000,000,000,000,000,000,000,000,000,000,000 years just for a 50% chance.
…Even with our billions of fast-typing Bobs.
Forget about all of Shakespeare’s works, let alone Hamlet. The universe won’t exist for that long.
The Final Ante
Luckily the scientists who made Bob have also made the Bob particle. The Bob particle is a massless particle that contains a Bob-monkey and our typewriter as one single entity.
As of 2006, the smallest distance measured is 1 attometer, which is 10−18 meters. The Bob particle has a diameter of 1 attometer.
The scientists have also developed a Bob Lattice, which has the length and width as our galaxy. It’s a single solid that contains Bob particles perfectly packed — there is absolutely no space between them. As our galaxy is roughly square 100,000 lightyears, our lattice contains 1073 Bob particles.
Also as of 2006, the smallest unit of time measured was 1 attosecond, the same scale, but for time — 10−18 seconds. A single Bob-particle produces a keystroke in this amount of time. Therefore, the collective Bob Lattice produces 101314 keystrokes in a second.
The previous population of second-generation Bobs took over 30 duodecillion years to get a 50% chance of having typed “ACTISCENEIELSINOREAPLATFORMBEFORETHECASTLE”. The Bob Lattice would collectively produce that text in:
of a second.
Bob Lattice vs. Complete Text of Hamlet
Hamlet contains 130,000 letters. Typing all of Hamlet correctly would require:
keystrokes
…for a 50% chance.
The Bob Lattice would take:
seconds
…to achieve that 50% chance.
…which is 10182632 seconds, or 10182624 years.
It’s clear that even the Bob Lattice, in it’s incalculably powerful production ability, cannot produce Hamlet anytime soon.
If the Bob Lattice started producing manuscript at the Big Bang, about 14 billion years ago, it would have produced a manuscript of 101331 letters. The chance that 130,000 of those letters have randomly aligned to be the full text of Hamlet is:
…which is 0.002%.
So, if you want Hamlet, or the full text of Shakespeare’s works, you’re going to have to wait… a long time.

Liked this Essay?
- You can get more Greatplay.net by looking at these categories: All, Mathematics.
- Or perhaps you'd be interested in a complete table of contents of all essays?
- You could also subscribing to the RSS feed, or use the sidebar to subscribe for email updates!
- Or you could follow me on Twitter or like me on Facebook
- If you feel particularly participatory, I'd love to hear from you. Feel free to leave a comment.



Really interesting read. Love the math posts. Keep it up.
2^21 is less than 3million and a LOT less than 308,915,776
(10^a)*(10^b)=10^(a+b) not 10^ab.
I have no idea where you got you 0.002% at the end at all, even using 10^ab
It looks like this post is using the old latex math engine, so I’ll have an excuse to edit the article anyway. I’ll check the math; Peter in 2010 was not very good at math. (Peter in 2011 is also not that good at math.)