I'm having trouble with this assignment that requires me to create 50 random unique numbers without using ArrayLists. I'm required to use a boolean array that checks whether the random number has already been generated. Each number that is generated out of 50 will be set to true in the boolean array. Ex. Generating a number 23 would make check[23]=true. The problem I am having is an error in the while loop that keeps on generating a new random number even if there is no other unique number left. How can I solve this problem while still using a boolean array to check uniqueness.
int rnd;
Random rand=new Random();Random rand=new Random();
int[] nums = new int[50];
boolean[] check = new boolean[51];
rnd = rand.nextInt(50) +1;
for (int k = 0; k<50; k++)
{
//Loop for when there number is already chosen
while (check[rnd]==true)
{
rnd = rand.nextInt(50) +1;
}
//Sets the random unique number to a slot in the array
if(check[rnd]==false)
{
nums[k]=rnd;
check[rnd]=true;
}
rnd = rand.nextInt(50) +1;
}
System.out.println(nums);