dimanche 1 février 2015

Print generated list of 20 random numbers + print same list again w/o duplicates

I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print the same list to screen again only skipping any number that has been already printed to the screen. Currently, my code generates 20 numbers from 1 to 20. I don't know how to print the same list as before only without duplicate numbers. I appreciate everyone who helps me in advance!



public void randomNumbers(){
System.out.println("Twenty random integers: ");
for (int x = 0; x < 20; x++){
int max = 20; //max value for range
int min = 1; //min value for range
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;//generates random # within range
System.out.println(randomNum);
}
System.out.println("Twenty random integers w/o duplicates: ");
}




Aucun commentaire:

Enregistrer un commentaire