lundi 30 novembre 2015

Adding code to random number generator to prevent duplicates [duplicate]

This question already has an answer here:

I need to get code that will allow me to remove duplicates of numbers as I have a random number generator which generates number associated to the counter of an array, the array being a 'pack' of cards, so I can't really afford to change the code but I really need to stop duplicates appearing!

    for (int v = 0; v < 30; v++)
    {
        Random rnd = new Random();
        rnd.nextInt(30);

        int i = rnd.nextInt(30);

        System.out.println(i + " " + aCards[i].getCard());
    }

This is what the code above produces, there are clearly duplicates within the list!




Aucun commentaire:

Enregistrer un commentaire