Monday, October 12, 2009

18 monkeys sitting at a keyboard

I have written a perl script that simulates eighteen monkeys each hitting one key either a letter of the alphabet or space key. They are trying to write the line " To be or not to be".

"Why?" I hear half the world scream, "For goodness sake, why? Think of the children!". Sorry slipped into a Simpson moment there.

Anyway I have written this totally geeky pointless script just because I can. It was a personal challenge to myself, can a mild mannered recruitment officer who knows a bit about perl write a script that will achieve this.

To explain the mechanics, I create a sequence of 18 random numbers between 1 and 27. Convert that all into letters or a space, then compare it to the text I want, calculate the percentage match and repeat until I get a decent match. For the maths bods out there the actual odds of hitting a perfect sequence are 18 to the power 27. In other words a really really really large number.

So anyway tonight I have been running batches of 2000000 iterations and outputting to a file every combination with a match over 7 letters.

Here are the results so far:

The highest ever match is 9 letters off a run of 200000000 iterations which just goes to show hitting the full eighteen letters could be a bit of a miracle. That run took 3 hours to do.

The interesting thing is that 8/9 letter is starting to make almost recognisable word patterns:

6533243 TULBEHXRBGMK EO BE 9
7596871 DO SJOR FOOUHU BO 8
15070898 AOSBH RQMNLT GO AA 8
41948431 TOPKGJCE ROTSTOJSE 8
83387651 TOEBOYXY UKT HP FE 8
131295542 NO BGVIR TPTGTX MB 8
123724196 TOIBXRY BENT TOWTE 8

I should explain the first number is the iteration, then we have the code sequence and finally the number of matching letters.

Ok so most of them still sound like the Russian or Japanese version of "to be or not to be" but bits are beginning to make sense.

Next experiment reduce the number of letters in the phrase to see if we can hit the high matches quicker. Maybe I will try "Out damned spot" but really I need something even shorter.

No comments: