A Million Monkeys making Shakespeare?

So I had a little time on my hands and thought it might be fun to get to the bottom of this Infinite Monkeys and Shakespeare dillema.

Here’s some python code that tries to randomly write the line “Alas, poor Yorick, I knew him, Horatio.” To make it easier I’ve ignored punctuation and case but still, you’re gonna need a lot of monkeys to get this level of randomness to happen.

#-------------------------------------------------------------------------------
# Name:        CreateShakespeare.py
# Purpose:     Simulate lots of monkeys banging on typewriters. Find out how long
#               it takes to create shakespeare. To make it easy we just want our
#               monkeys to write the line
#               "Alas, poor Yorick, I knew him, Horatio"
#               And we'll ignore the punctuation and case to make it easier for
#               our tireless computer monkeys.
#
# Author:      jbloom
#
# Created:     07/11/2006
#-------------------------------------------------------------------------------
import random
import string
import time
 
correctText = 'Alas poor Yorick I knew him Horatio'
charList = string.ascii_uppercase + ' '
startTime = time.localtime()
endTime = None
currentLoopCounter = 0
 
def getRandomCharacter():
    return charList[random.randrange(0,27)]
 
def createRandomLine():
   '''Creates the Random Line by monkey power'''
   randomLine = []
   for char in xrange(len(correctText)):
   randomLine.append(getRandomCharacter())
   return ''.join(randomLine)
 
def testLine(inputLine):
    '''Tests the passed line to see if we got the correct text '''
    if inputLine == correctText.upper():
        return True
    else:
        return False
 
if __name__ == "__main__":
    #print testLine('ALAS POOR YORICK I KNEW HIM HORATIO')
    while True:
       currentLoopCounter += 1
       line = createRandomLine()
       if testLine(line) == True:
           endTime = time.localtime()
           print ('')
           print ('Started: %s' % startTime)
           print ('Ended  : %s' % endTime)
           print ('Total Iterations: %s' % currentLoopCounter)
           print ('MONKEY  LINE: %s' % line)
           print ('DESIRED LINE: %s' % correctText)
           break
      else:
           print line, currentLoopCounter
Posted: February 14th, 2007
Categories: Code, python
Tags:
Comments: 6 Comments.
Comments
Comment from jbloom - February 14, 2007 at 1:32 am

Wordpress seems to be killing my python indentation. So that example would be broken :(

You can download a working version from here if you need.
http://www.uselesspython.com/download.php?script_id=174

Comment from Kyle - February 15, 2007 at 4:10 pm

So…what happened when you ran the script? How many monkeys did it take?

Comment from jbloom - February 15, 2007 at 4:40 pm

Well it will take quite a lot. Here’s how it breaks out:

Quoting from Wikipedia. http://en.wikipedia.org/wiki/Infinite_monkey_theorem

“Ignoring punctuation, spacing, and capitalization, a monkey typing letters uniformly at random has one chance in 26 of correctly typing the first letter of Hamlet. It has one chance in 676 (26 times 26) of typing the first two letters. Because the probability shrinks exponentially, at 20 letters it already has only one chance in 26 to the 20th power = 19,928,148,895,209,409,152,340,197,376, roughly equivalent to the probability of buying 4 lottery tickets consecutively and winning the jackpot each time.”

In our case the “alas poor yorick” string is 38 characters long. So the probability is 27 (including the Space Character) to the 38th power. Roughly 1 in 246,503,470,495,806,750,399,613,145,337,394,381,307,472,651,239,760,0969
chances.

I’m not sure there’s enough time left in our universe for this to finish.

Comment from Kyle - March 20, 2007 at 12:12 pm

Well, let us know.

Comment from Imran Chaudhry - July 31, 2009 at 8:07 am

Yes, do let us know how it turns out.

Does the theorem take in to account the evolution of monkeys over the infinite time span?

Comment from jbloom - August 2, 2009 at 9:30 pm

Good question Imran,

I didn’t actually consider the monkeys evolving, but I certainly should have. If we allow our monkeys to evolve over an infinite span of time they could certainly surpass human levels of intelligence relatively quickly. The question would then be would they realize they were part of a thought experiment and then refuse to type any longer?
If they did this there would be no chance of ever printing the line in question.

-josh