Hi all,

I am creating a Genetic Algorithm, and I have two Arrays contain int values, I would like to know a easy way of Split each array into 4 random sections e.g.

Array 1 = [1| 4 5 3| 1 4 |6 7 8| 1]
Array 2 = [2| 6 1 9| 2 4 |1 3 1| 0]

and then create 4 Arrays from the two randomly picking ever a bit from the Array1 or Array 2.

Example:

Array3 = [1|6 1 9| 14 |6 7 8|0]
Array4 = [1|4 5 3|2 4 |6 7 8|1]

Hope this all makes sense.

Kind Regards

FlippA

I used something like this to get the index of the split:

``````Random rand = new Random();
int [] a = {1,4,5,3,1,4,6,7,8,1};

int [] spl = new int[4];
spl[0] = rand.Next(1, a.Length - 2);
spl[1] = rand.Next(1, a.Length - spl[0] - 1);
spl[2] = rand.Next(1, a.Length - spl[0] - spl[1]);
spl[3] = a.Length - spl[0] - spl[1] - spl[2];``````

accounting for the fact that each split had to hold at least one number. I hope that's the setup you had in mind with the 4 partitions randomly along the array.

After that it should be easy to get the subarrays (perhaps having lists might make things even easier)

Use LINQ Skip() and Take().

``````..
int[] p = ar.Skip(skip).Take(rnd).ToArray();
...``````
commented: Works :) +2

Ok i have the arrays slit up into section how what is the best way of joining them together?

Many Thanks for the fast replies

Have a go for yourself first. The best way to learn is by trying.
Just think it through step by step. For each section you need to pick from either array 1 or array 2.
So section 1 will be either array1[0] OR array2[0], section 2 will be array1[1] OR array2[1].
Then piece together whats left for your second gene.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.