CodeMusings

Kick back and get started at your own pace.

Skip to Content

Dawkins Weasel Simulation

Methinks it is like a weasel
— Hamlet, 3.2.379

In The Blind Watchmaker, Richard Dawkins wrote of a thought experiment that was based on the infinite monkey theorem, which states that a monkey hitting random keys on a keyboard, given infinite time, will almost surely type a given text. Usually, the complete works of Shakespeare are alluded to. The term almost surely is referenced in the way it is used in probability theory.

Dawkins referenced the theorem in response to the misconception that natural selection, the driving force behind adaptation, is driven by purely random chance. The key factor that shapes how a population adapts is that the traits most likely to be passed on are those which are best suited for the environment in which a population finds itself. To illulstrate the key roll the environment plays in natural selection, Dawkins limits the the infinite monkey theorem such that the monkey must only produce a small passage of dialogue from Hamlet, and the keyboard layout is limited to only capital letters and a space bar. Now, we need to simulate adaptation. As an analogue to the environment, a target phrase is selected, such as METHINKS IT IS LIKE A WEASEL. We start with a randomly generated string as the parent generation. Each successive generation has a 5% chance of a letter mutating and is scored by how closely it matches the target phrase. The population with the highest score is selected to "breed" for the next generation. Further, this implementation allows for offspring to mutate letters that are already "correct."

Given a 27-character alphabet and that the phrase to produce is 28 characters, simple combinatorics shows the possible combinations totals to $28^{27}$, or about $1.2\times10^{39}$. When factoring in the effect that the environment has in shaping the evolution of a species, the outcome is not based purely on chance. Life is plastic to its environment. A simulation of the Dawkins Weasel program is below.