Attempt at infinite monkey theorem using python

The infinite monkey theorem states that a monkey randomly pressing keys on a typewriter keyboard for an infinite amount of time will eventually type out the complete works of Shakespeare. While this may seem highly improbable, it raises an interesting question – can we simulate this scenario using Python?

Option 1: Randomly Generating Strings

One way to approach this problem is by randomly generating strings of characters and checking if they match the desired output. Here’s a sample code that demonstrates this approach:


import random
import string

def generate_random_string(length):
    return ''.join(random.choices(string.ascii_lowercase + ' ', k=length))

def attempt_infinite_monkey_theorem(target):
    attempts = 0
    while True:
        attempts += 1
        random_string = generate_random_string(len(target))
        if random_string == target:
            return attempts

target_string = "to be or not to be"
result = attempt_infinite_monkey_theorem(target_string)
print(f"Number of attempts: {result}")

This code generates random strings of lowercase letters and spaces using the `generate_random_string` function. It then compares each generated string with the target string and returns the number of attempts it took to match the target.

Option 2: Brute Force Approach

Another approach is to use a brute force algorithm that iterates through all possible combinations of characters until it finds a match. Here’s a sample code that demonstrates this approach:


import itertools

def attempt_infinite_monkey_theorem(target):
    attempts = 0
    for length in range(1, len(target) + 1):
        for combination in itertools.product(string.ascii_lowercase + ' ', repeat=length):
            attempts += 1
            random_string = ''.join(combination)
            if random_string == target:
                return attempts

target_string = "to be or not to be"
result = attempt_infinite_monkey_theorem(target_string)
print(f"Number of attempts: {result}")

This code uses the `itertools.product` function to generate all possible combinations of lowercase letters and spaces. It then compares each combination with the target string and returns the number of attempts it took to match the target.

Option 3: Markov Chain Approach

A more sophisticated approach is to use a Markov chain to simulate the monkey’s typing behavior. This involves analyzing a given text to determine the probability of transitioning from one character to another and using this information to generate new strings. Here’s a sample code that demonstrates this approach:


import markovify

def attempt_infinite_monkey_theorem(target):
    attempts = 0
    text_model = markovify.Text(target)
    while True:
        attempts += 1
        random_string = text_model.make_sentence()
        if random_string == target:
            return attempts

target_string = "to be or not to be"
result = attempt_infinite_monkey_theorem(target_string)
print(f"Number of attempts: {result}")

This code uses the `markovify` library to create a Markov chain model based on the target string. It then generates new strings using this model and compares each generated string with the target string until a match is found.

After considering these three options, the best approach depends on the specific requirements of the problem. If the target string is relatively short and the desired outcome is to find a match quickly, the brute force approach (Option 2) may be the most suitable. However, if the target string is longer and the goal is to simulate the monkey’s typing behavior more accurately, the Markov chain approach (Option 3) would be a better choice. The randomly generating strings approach (Option 1) can be a simple and quick solution, but it may not guarantee finding a match within a reasonable time frame.

Rate this post

2 Responses

  1. Wow, I never thought monkeys and programming would go hand in hand! 🐒🖥️ But seriously, which approach is the most effective in this case? 🤔

Leave a Reply

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

Table of Contents