package org.nusco.swimmers.creature.genetics;

public class DNA {
	public static final int MIRROR_ORGAN = 0b00000001;
	public static final double MUTATION_RATE = 0.03;

	private int[] genes;

	public DNA(int... genes) {
		this.genes = normalize(genes);
	}

	private int[] normalize(int[] genes) {
		int[] result = new int[genes.length];
		for (int i = 0; i < genes.length; i++) {
			int normalized = genes[i] % 256;
			if(normalized < 0)
				result[i] = -normalized;
			else
				result[i] = normalized;
		}
		return result;
	}

	public int[] getGenes() {
		return genes;
	}

	public DNA mutate() {
		int[] resultGenes = new int[genes.length];
		for (int i = 0; i < resultGenes.length; i++) {
			if(Math.random() < MUTATION_RATE)
				resultGenes[i] = mutate(genes, i);
			else
				resultGenes[i] = genes[i];
		}
		return new DNA(resultGenes);
	}

	private int mutate(int[] resultGenes, int i) {
		int randomFactor = ((int)(Math.random() * 40)) - 20;
		return resultGenes[i] + randomFactor;
	}

	public static DNA random() {
		final int genomeSize = 60 * Byte.SIZE;
		int[] genes = new int[genomeSize];
		for (int i = 0; i < genes.length; i++) {
			genes[i] = rnd(0, 255);
		}
		return new DNA(genes);
	}

	public static DNA ancestor() {
		int NOT_MIRRORING = 0;
		int[] genes =  new int[]{
									NOT_MIRRORING, 60, 40, 0, 0,
									NOT_MIRRORING, 50, 60, 30, 0,
									NOT_MIRRORING, 50, 60, 30, 0,
									NOT_MIRRORING, 30, 50, 30, 0,
									NOT_MIRRORING, 30, 50, 30, 0,
									NOT_MIRRORING, 30, 50, 30, 0,
									NOT_MIRRORING, 30, 50, 30, 0,
									NOT_MIRRORING, 50, 40, 20, 123,
									NOT_MIRRORING, 50, 40, 20, 123,
									NOT_MIRRORING, 50, 40, 20, 123,
									NOT_MIRRORING, 50, 40, 20, 123
								};
		return new DNA(genes);
	}

	private static int rnd(int min, int max) {
		return (int)(Math.random() * (max - min)) + min;
	}
}

@@ -40,16 +40,42 @@
 etics;%0A%0A
+import java.util.Random;%0A%0A
 public c
@@ -1048,158 +1048,106 @@
 %7B%0A%09%09
-final int genomeSize = 60 * Byte.SIZE;%0A%09%09int%5B%5D genes = new int%5BgenomeSize%5D;%0A%09%09for (int i = 0; i %3C genes.length; i++) %7B%0A%09%09%09genes%5Bi%5D = rnd(0, 255
+long seed = new Random().nextLong();%0A%09%09System.out.println(%22Generate DNA based on seed: %22 + seed
 );%0A
-%09%09%7D%0A
 %09%09re
@@ -1151,35 +1151,34 @@
 %09return 
-new DNA(genes
+random(seed
 );%0A%09%7D%0A
+%09
 %0A%09public
@@ -1208,504 +1208,288 @@
 %7B%0A%09%09
-int NOT_MIRRORING = 0;%0A%09%09int%5B%5D genes =  new int%5B%5D%7B%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 60, 40, 0, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 50, 60, 30, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 50, 60, 30, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 30, 50, 30, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 30, 50, 30, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 30, 50, 30, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 30, 50, 30, 0,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 50, 40, 20, 123,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 50, 40, 20, 123,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 50, 40, 20, 123,%0A%09%09%09%09%09%09%09%09%09NOT_MIRRORING, 50, 40, 20, 123%0A%09%09%09%09%09%09%09%09%7D;
+return DNA.random(9018779372573137080L);%0A%09%7D%0A%0A%09private static DNA random(long seed) %7B%0A%09%09Random random = new Random(seed);%0A%09%09final int genomeSize = 60 * Byte.SIZE;%0A%09%09int%5B%5D genes = new int%5BgenomeSize%5D;%0A%09%09for (int i = 0; i %3C genes.length; i++) %7B%0A%09%09%09genes%5Bi%5D = rnd(0, 255, random);%0A%09%09%7D
 %0A%09%09r
@@ -1554,16 +1554,31 @@
  int max
+, Random random
 ) %7B%0A%09%09re
@@ -1584,35 +1584,41 @@
 eturn (int)(
-Math.
 random
+.nextDouble
 () * (max - 

Create new ancestor