lundi 2 décembre 2019

Looking for a way to not include repeats for a series of random numbers

I have been working on a bingo game for class and I have a problem with repeating numbers. I understand how to create non-repeating random numbers with arrays but not for a normal integer or string. I will show an example of what I am going for.

String var = null;
        int ranVar;
        final Random ran = new Random();
        ranVar = ran.nextInt(74) + 1;
        boolean match;
// Attempted the code for non-repeating numbers here:
        for (int i = 0; i < 1; i++) {
            if (ranVar <= 15) {
                match = false;
                return var = "B" + ranVar;
                for (int x = 0; x <= i; x++) {
                    if (ranVar == x) {
                        match = true;
                    }
                }
                if (match == true) {
                    i = i - 1;
                } else ranVar = i;
            }else if (ranVar <= 30) {
                return var = "I" + ranVar;
            } else if (ranVar <= 45) {
                return var = "N" + ranVar;
            } else if (ranVar <= 60) {
                return var = "G" + ranVar;
            } else if (ranVar <= 75) {
                return var = "O" + ranVar;
            }

        }

As you can see I have attempted to do this with the B value, but was unsuccessful. Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire