vendredi 4 août 2017

Draw random numbers from different columns (blocks) in Java

I want to draw 6 different random numbers between 1 to 49 and make sure that they are neither drawn from one single block (cf. the 7 blocks below), nor from 2 blocks, nor from 6 blocks and nor from 7 different blocks. If 6 random numbers are drawn in such a way, 6 new random numbers should be redrawn. In other words: The 6 random generated numbers should be drawn from 3, 4 or 5 blocks - does not matter which block.

enter image description here

What I have tried is the following:

public class CountColumns {

    public static void main(String[] args) {

        ArrayList<Integer[]> list = new ArrayList<Integer[]>();
        list.add(new Integer[] { 1, 8, 15, 22, 29, 36, 43 });
        list.add(new Integer[] { 2, 9, 16, 23, 30, 37, 44 });
        list.add(new Integer[] { 3, 10, 17, 24, 31, 38, 45 });
        list.add(new Integer[] { 4, 11, 18, 25, 32, 39, 46 });
        list.add(new Integer[] { 5, 12, 19, 26, 33, 40, 47 });
        list.add(new Integer[] { 6, 13, 20, 27, 34, 41, 48 });
        list.add(new Integer[] { 7, 14, 21, 28, 35, 42, 49 });

        //Random 6 numbers -- For testing purposes
        Set<Integer> mySet = new HashSet<Integer>(Arrays.asList(9, 16, 12, 40, 7, 49));

        int countblock1 = 0;
        int countblock2 = 0;
        int countblock3 = 0;
        int countblock4 = 0;
        int countblock5 = 0;
        int countblock6 = 0;
        int countblock7 = 0;

        for (int i = 0; i < list.size(); i++) {
            for (Integer j : mySet) {
                for (int c = 0; c < list.get(i).length; c++) {
                    if (j == list.get(i)[c]) {
                        if (i == 0) {
                            countblock1++;
                        } else if (i == 1) {
                            countblock2++;
                        } else if (i == 2) {
                            countblock3++;
                        } else if (i == 3) {
                            countblock4++;
                        } else if (i == 4) {
                            countblock5++;
                        } else if (i == 5) {
                            countblock6++;
                        } else if (i == 6) {
                            countblock7++;
                        }
                    }
                }
            }
        }

This code helps me to count how many numbers fall within a block. I am well aware, I will need some more sophisticated code - maybe combinatorics or permutation would help but I am not sure though. Can anybody help me out? Many thanks.




Aucun commentaire:

Enregistrer un commentaire