// random chance that a bit is flipped private static Chromosome mutateGenes(Chromosome toMutate, double mutationRate) { double target = 1.0 / mutationRate; if (target == Program.rng.Next(1000) + 1) { // mutation! now we randomly select bit to change int geneToMutate = Program.rng.Next(toMutate.getNumGenes()); int bitsCalc = maxGeneVal; int bitPos = 0; while (bitsCalc != 1) { bitsCalc = bitsCalc >> 1; bitPos++; } int bitToFlip = Program.rng.Next(bitPos); uint newGeneVal = toMutate.getGenes()[geneToMutate]; newGeneVal ^= (uint)(1 << bitToFlip); toMutate.setGene(geneToMutate, newGeneVal); } return toMutate; }