lundi 4 mai 2015

Java RNG check for duplicate

// randomly select 5 numbers
    for(int i=0;i<maxNumbers;i++)
    {
        int r = rgen.nextInt(100) + 1;
        s += r + ",";
        randomNumbers.add(String.valueOf(r));
    }

   // check if this item in number list
                    if(randomNumbers.contains(item))
                    {

                        randomNumbers.remove(item);
                    }

I am able to generate the 5 random numbers from an array list of 1-100, but I am unable to get the randomNumbers.remove(item) to prevent a duplicate entry. I added partial code to not make it super bulky. Any help or suggestions on how to not duplicate numbers?




Aucun commentaire:

Enregistrer un commentaire