Does this method check and replace randoms in the array?
public void generateNumbers()
{
for(int counter=0; counter< 6; counter++){
numbers[counter]= 1+ (int)(Math.random()*49);
for(int y = counter - 1; y >=0 ; y--){
if(numbers[counter] == numbers[y]){
numbers[counter]= 1+ (int)(Math.random()*49);
}
}
}
If there is duplicates I need to replace this with another random number and then check again. I want to check this using a loop if possible and not using collections.
The range of the numbers created by random should be 1 – 49.
You need to “rewind” back to
counter-1on finding the duplicate, so that you start searching for duplicates again:Your original method would fail if you start with this array
then generate 3, check it against 8 and 7, find duplicate 3, and generate 8 as a replacement. At this point, your algorithm would check 8 against 1, and be satisfied that there’s no duplicate; this would be incorrect.