private int[] getPairing(int chr, int side) {
        assert(popdata.ploidy > 2);
        int[] pairedWithSide = new int[popdata.ploidy];
        boolean done[] = new boolean[popdata.ploidy];
        TetraploidChromosome tchrom = (TetraploidChromosome)popdata.getChrom(chr);
        if (genomehs == null) calcGenomehs();
        calcGenohs(chr,side);
        int firsth = 0, secondh,  //first and second haplostruct in initorder forming a pair
                gnm, //genome number at chrom [initorder[firsth]], side=side
                hIndex; //index within genohs.get(gnm)
        while (firsth < popdata.ploidy-1) {
            while (firsth < popdata.ploidy-1 && done[firsth]) firsth++;
            if (firsth < popdata.ploidy-1) {
                done[firsth] = true;
                gnm = genomenr[chr][side][initorder[firsth]];
                hIndex = genohs.get(gnm).indexOf(initorder[firsth]);
                genohs.get(gnm).remove(hIndex);
                if ( !genohs.get(gnm).isEmpty() &&
                        rand.nextDouble()<tchrom.getPrefPairingProb() ) {
                    //preferential pairing, get a partner with same genome number:
                    hIndex = rand.nextInt(genohs.get(gnm).size());
                    secondh = backorder[genohs.get(gnm).get(hIndex)];
                } else {
                    //no preferential pairing: get any still available partner
                    secondh = firsth;
                    while (done[secondh]) {
                        secondh = rand.nextInt(popdata.ploidy-firsth-1)+firsth+1;
                    }
                    gnm = genomenr[chr][side][initorder[secondh]];
                    hIndex = genohs.get(gnm).indexOf(initorder[secondh]);
                }
                done[secondh] = true;
                genohs.get(gnm).remove(hIndex);
                pairedWithSide[firsth] = secondh;
                pairedWithSide[secondh] = firsth;
                firsth++;
            }    
        } //while firsth
        //} //preferential pairing    
        return pairedWithSide;
    } //getPairing