A while back I came across an article that mentioned genetic algorithms. For those unfamiliar with the term, simply put, rather than finding a solution to a problem by iterating over all possible cases, a genetic algorithm attempts to find a solution by starting with a guess, then generating a next guess by applying numerous random changes to the current guess and picking the guess that best fits the solution as the next best guess. Rinse and repeat.
Unfortunately, most of the articles and examples I came across involved far too much tech speak and math for a brain long removed from theoretical mathematics. I put it aside for a while until I read a paper by Richard Dawkins. He discussed the "Methinks it is like a weasel" algorithm in such clear terms that I coded it up in vbScript. It is such a simple example of a genetic algorithm that I decided to post it here.
In basic terms we define a target which is the string, "METHINKS IT IS LIKE A WEASEL". We generate ten new strings by applying a random mutation to the parent string. A mutation consists of possibly replacing a randomly selected letter with a new randomly generated letter. From the ten children we select the child that is the closest match to the target string. This child becomes the new parent for the next generation. If you run the script you will see how quickly we resolve to the target string.
Prior to running this script you should set cscript.exe as the default script engine by typing
cscript //h:cscript //s
otherwise each wscript.echo
will pop up a message box. If you do not want to reset the default then run the script by
cscript weasel.vbs
This script uses only upper case letters and spaces but could be modified to include lower case letters and punctuation.