vendredi 13 mars 2015

Generating random numbers without duplicates [duplicate]


This question already has an answer here:




As the title says I'm trying to get my code to not generate the same number twice in a set array. EX: (comp is an array defined as int[] comp = new int[20];)



Random ran = new Random();
for(int i = 0; i < comp.length; i++)
{
comp[i] = ran.nextInt(80)+1;
for(int j = 0; j < comp.length; j++)
{
if(comp[i] == comp[j])
comp[i] = ran.nextInt(80)+1;
}
System.out.println(comp[i]);
}




Aucun commentaire:

Enregistrer un commentaire