Infinite Monkey Experiment

Ah, finally a fun one.

Recently, my tiktok feed has been filled with jokes regarding the Infinite Monkey Theorem. Essentially the plot of this theorem states that if you had monkeys typing utterly randomly on a typewriter for an infinite amount of time, the monkeys would end up outputting the complete works of Shakespeare and any other texts ever written. In this project, I replicated this experiment in Python by creating a list of characters plus a space and had a script pull from these characters at random.

In this experiment I learned how to read and write to a file, so there’s the benefit of this project. The entire project is here in GitHub if you’d like to follow along.

Monkey Script

To begin, I needed to create a script. At the top of this script I imported random:

import random

Then I created an array of options so I could randomly pull characters:

import random

options = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," "]

Next, I started a while loop and created a random value that would pull from the options:

import random

options = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," "]

while(True):
    input = random.choice(options)

Finally, instead of printing the text which would take up too much space in ram, I had to create a script and then write to it:

import random

options = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," "]

while(True):
    input = random.choice(options)
    script = open("Script.txt", "a")
    script.write(input)

open creates a new text file, and “a” means append which means it will just keep writing to the document instead of overwriting it.

Reading Through the Text File

Now I needed a script that would check the file that the other script outputted, looking for English words. Using a library called english_words, I imported a list of lowercase alpha words using:

from english_words import english_words_lower_alpha_set

Next I needed to open the file and read it as an array. Originally the document is recognized as a simple string, so I use the split method to split it at each space:

from english_words import english_words_lower_alpha_set

with open('Script.txt','r+') as Script:
    lines = Script.read().split(" ")

Then a for loop was needed to check each word in the split list, and then check whether or not the word is in the english_word list:

from english_words import english_words_lower_alpha_set

with open('Script.txt','r+') as Script:
    lines = Script.read().split(" ")
    for word in lines:
        if(word in english_words_lower_alpha_set):

For simplicities sake, I made it so the script only recognized words with more than 2 characters:

from english_words import english_words_lower_alpha_set

with open('Script.txt','r+') as Script:
    lines = Script.read().split(" ")
    for word in lines:
        if(word in english_words_lower_alpha_set):
            if(not len(word) <= 2):

Finally, the script writes to a new file:

from english_words import english_words_lower_alpha_set

with open('Script.txt','r+') as Script:
    lines = Script.read().split(" ")
    for word in lines:
        if(word in english_words_lower_alpha_set):
            if(not len(word) <= 2):
                foundWords = open("WordsFound.txt", "a")
                foundWords.write(f"{word}\n")

Summary

Now, once the first script runs, I can run the reading script in order to find the words that have been printed in the document. One day,

One thought on “Infinite Monkey Experiment

Leave a Reply

Your email address will not be published. Required fields are marked *