dimanche 1 février 2015

Fill array w/ 20 random #s but no repeats within the numbers [duplicate]


I want to fill up an array with 20 random integers (from 1 to 100), however there can be no repeats within the 20 random numbers. Then, I want to print the result to the screen. Right now, my code generates 20 random numbers and stores them in an array. I don't know how to make it so that the numbers within the array aren't duplicated. Thanks to everyone who takes the time to help.



System.out.println("Twenty random integers: ");
int max = 100; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int[] all = new int[20];
for (int x = 0; x < 20; x++) {
int randomNum = rand.nextInt((max - min) + 1) + min;
System.out.println(randomNum);
all[x] = randomNum;
}




Aucun commentaire:

Enregistrer un commentaire