// polar form of the Box-Muller transformation double random_get_gaussian() { double x1, x2, w; do { x1 = 2.0 * random_get_uniform() - 1.0; x2 = 2.0 * random_get_uniform() - 1.0; w = x1 * x1 + x2 * x2; } while (w >= 1.0); return x1 * sqrt(-2.0 * log(w) / w); }
// mutate genes with given probability and deviation void genotype_mutate(Genotype g) { int i; for (i = 0; i < genotype_size; i++) if (random_get_uniform() < MUTATION_PROBABLITY) g->genes[i] += random_get_gaussian() * MUTATION_DEVIATION; }