-1

I am learning how to code and my teacher gave me an exercise to create a lottery program that generates 6 random numbers between 1 and 49 with no duplicates and one bonus number that could be a duplicate. My program generates all the numbers fine, but for some reason duplicates still appear. Could someone please explain why the code that checks for duplicates doesn't work, as I have been struggling to understand why it doesn't work. Please bear in mind that I'm a new programmer so try and keep explanations beginner friendly. Thanks in advance.

int[] lottonums = new int[6];


//Generates 6 random numbers between 1-49
for(int i = 0; i < lottonums.length; i++){
    lottonums[i] = (int)(Math.random()* 49 +1);
}


//Checks for duplicates   
for(int x = 0; x < 6; x ++){
    for(int y = x + 1; y < 6; y ++){
        while(lottonums[x] == lottonums[y]){
            lottonums[y] = (int)(Math.floor(Math.random() * 49 + 1));
        }
    }

}

//Bonus ball, no checks for duplicates 
int bonusBall = (int)(Math.random() * 49 + 1);

Arrays.sort(lottonums);
System.out.println("\nThe lottery numbers are: ");

for( int nu = 0; nu < lottonums.length; nu ++){
    System.out.print(lottonums[nu] + " " );
}
System.out.println("\nThe bonus number is: " + bonusBall + "\n");

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
F_martn
  • 11
  • 3
    It doesn't work because simply trying to re-generate some of the numbers does not guarantee there won't be duplicates again. What you could do is generate a list of all numbers from 1 to 49, [shuffle it](https://stackoverflow.com/questions/16000196/java-generating-non-repeating-random-numbers) and take the first 6 numbers from the resulting list. – Federico klez Culloca Aug 11 '22 at 10:06
  • I think your check for duplicates should go into into the `for` loop that generates the lotto numbers but before the new lotto number is applied to the array (this would also include the bonus): `for (int i = 0; i < lottonums.length + 1; i++){ int num = (int)(Math.random()* 49 +1); //check for duplicate in array here. If not then if (i < lottonums.length) { lottonums[i] = num; } else { bonusBall = num; } }` – DevilsHnd - 退職した Aug 11 '22 at 10:30
  • @DevilsHnd that looks like an answer rather than a comment :) – Federico klez Culloca Aug 11 '22 at 10:32

1 Answers1

0

Best Way to have unique number is by using Set instead of array. if you are not aware much about set have a look into it set TreeSet

Basically if you look at your code

//Checks for duplicates
        for(int x = 0; x < 6; x ++){
            for(int y = x + 1; y < 6; y ++){
                while(lottonums[x] == lottonums[y]){
                    //below line does not gurrantee its going to insert unique number
                    //example [1,2,6,6] here at index 2 and 3  6 is there 
                    //now you got this while checking duplicate 
                    //after you are generating new random suppose new generated number is 2 
                   // as you are not going back to check duplicate so it will be inserted
                    lottonums[y] = (int)(Math.floor(Math.random() * 49 + 1));
                }
            }

        }

you can try the below solution using set which fits yous requirement

// TreeSet will have unique element in sorted manner no need to sort again
        TreeSet<Integer> set=new TreeSet<>();

        int n=6;
        while(set.size()<n-1)
        {
            set.add((int)(Math.random()* 49 +1));
        }

//Bonus ball, no checks for duplicates
        int bonusBall = (int)(Math.random() * 49 + 1);

        System.out.println("\nThe lottery numbers are: ");

        for( int nu :set){
            System.out.print(nu + " " );
        }
        System.out.println("\nThe bonus number is: " + bonusBall + "\n");
    }
bhaskarkh
  • 179
  • 2
  • 11