private HaploStruct[] patternsToGametes(HaploStruct[] patterns, int[] centro)
            throws Exception {
        //sort the patterns such that the four gametes
        //are based on the centromere alleles, in the order of centro:
        HaploStruct tmphs;
        for (int s=0; s<patterns.length-1; s++) {
            if (patterns[s].getFounderAtCentromere() != centro[s]) {
                int t = s+1;
                while (patterns[t].getFounderAtCentromere() != centro[s]) {
                    t++;
                }
                tmphs = patterns[s];
                patterns[s] = patterns[t];
                patterns[t] = tmphs;
            }
        }
        /*
         * Note that in a quadrivalent the centro array contains two 0's, 
         * two 1's, two 2's and two 3's, and that the two chromatids with
         * the same centromere allele are not yet randomized. We do that now:
         */
        if (patterns.length>4) {
            for (int all=0; all<4; all++) {
                int first, second;
                if (rand.nextBoolean()) {
                    first=0; while (centro[first]!=all) first++;
                    second=first+1; while (centro[second]!=all) second++;
                    tmphs = patterns[first];
                    patterns[first] = patterns[second];
                    patterns[second] = tmphs;
                }
            }
        }
        

        //finally construct the gametes based on the slots structure:
        HaploStruct[] gametes = new HaploStruct[patterns.length];
        for (int s=0; s<patterns.length; s++) {
            gametes[s] = fillGamete(patterns[s]);
        }
        return gametes;
    } //patternsToGametes