Mutations are random
Mutations can be beneficial, neutral, or harmful for the organism, but mutations do not "try" to supply what the organism "needs." Factors in the environment may influence the rate of mutation but are not generally thought to influence the direction of mutation. For example, exposure to harmful chemicals may increase the mutation rate, but will not cause more mutations that make the organism resistant to those chemicals. In this respect, mutations are random whether a particular mutation happens or not is unrelated to how useful that mutation would be.
For example, in the U.S. where people have access to shampoos with chemicals that kill lice, we have a lot of lice that are resistant to those chemicals. There are two possible explanations for this:
Resistant strains of lice were always there and are
just more frequent now because all the non-resistant lice died
a sudsy death.
Exposure to lice shampoo actually caused mutations
for resistance to the shampoo.
Scientists generally think that the first explanation is the right one and that directed mutations, the second possible explanation relying on non-random mutation, is not correct.
Researchers have performed many experiments in this area. Though results can be interpreted in several ways, none unambiguously support directed mutation. Nevertheless, scientists are still doing research that provides evidence relevant to this issue.
In addition, experiments have made it clear that many mutations are in fact random, and did not occur because the organism was placed in a situation where the mutation would be useful. For example, if you expose bacteria to an antibiotic, you will likely observe an increased prevalence of antibiotic resistance. Esther and Joshua Lederberg determined that many of these mutations for antibiotic resistance existed in the population even before the population was exposed to the antibiotic and that exposure to the antibiotic did not cause those new resistant mutants to appear.
The Lederberg experiment
In 1952, Esther and Joshua Lederberg performed an experiment that helped show that many mutations are random, not directed. In this experiment, they capitalized on the ease with which bacteria can be grown and maintained. Bacteria grow into isolated colonies on plates. These colonies can be reproduced from an original plate to new plates by "stamping" the original plate with a cloth and then stamping empty plates with the same cloth. Bacteria from each colony are picked up on the cloth and then deposited on the new plates by the cloth.
Esther and Joshua hypothesized that antibiotic resistant strains of bacteria surviving an application of antibiotics had the resistance before their exposure to the antibiotics, not as a result of the exposure. Their experimental set-up is summarized below:
So the penicillin-resistant bacteria were there in the population before they encountered penicillin. They did not evolve resistance in response to exposure to the antibiotic.