I am a bit new to Java so I hope I can get some help here. I created a random number generator that gets told how many numbers it's supposed to generate. Problem is that my version allows for duplicates which I do not want.
public int[] rng()
{
Scanner scan1 = new Scanner(System.in);
System.out.println("How many numbers do you want generated?");
int rng_length = scan1.nextInt();
System.out.println("Number range");
int rng_range = scan1.nextInt();
scan1.close();
int anArray [] = new int[rng_length];
for (int i = 0; i < anArray.length; i++)
{
anArray[i] = (int) (Math.random()* rng_range) + 1;
}
Arrays.sort(anArray);
System.out.println(Arrays.toString(anArray));
return anArray;
}
What do I need to change in order to avoid duplicates? Preferably I'd want the array to be created, then analyzed for duplicates and those duplicates get re-rolled till they aren't any duplicates any longer but I have no clue how to do that.