A Million Monkeys making Shakespeare?
Feb 14th, 2007 by jbloom
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
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
So…what happened when you ran the script? How many monkeys did it take?
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.
Well, let us know.