Good point on the math test thing Katie, props.
Evolution works very much like simulated annealing in math and computer science, hence why the latter is called a "genetic algorithm". If a randomly developed trait leads to better survival then it is adopted, but if the trait leads to less survivability it is not necessarily discarded, but it is accepted with a certain probability (i.e. the probability that the branch with decreased survivability doesn't die off) and may lead to even more survivability in the long run. Randomized trial and error, loosely guided to a certain goal. It may seem a bit unlikely, but in this mixed up universe of ours it works. Beautiful, no?