0

I have a problem in generating the non-duplicate numbers. I tried to do the do-while loop but it seems not working. How do I fix it?

import java.util.Arrays;

public class Assignment2_Q2 {
    
    public static void main (String[] args){
        
        int[] myList = new int[10];
        
        number(myList);
        sortedOrder(myList);
        display (myList);
    }
        
    public static void number(int[] list){
            
        int random;
        
        random = (int)(Math.random() * 21);
        list[0] = random;
            
        for (int i = 1; i < list.length; i++){
            do{
                random = (int)(Math.random() * 21);
                list[i] = random;        
            }while(list[i] == list[i-1]);
        }
    }
    
    public static void sortedOrder(int[] list){
        
        java.util.Arrays.sort(list);    
    }
    
    public static void display(int[] list){
        
        System.out.println("The array in the sorted order:\n" + Arrays.toString(list) + "\n");  
    }             
}

Example output:

The array in the sorted order:
[0, 0, 6, 7, 13, 16, 16, 18, 19, 20]

As you can see, 0 and 16 come twice each. They are duplicates. I have also seen a number coming three times in one run.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • So you need all numbers `1-20` in a random order? – Bohemian Jan 15 '23 at 07:09
  • @Bohemian No he wants 10 unique ones – Gabe Sechan Jan 15 '23 at 07:11
  • This and similar questions have been asked a thousand times. Search for more. How do I ask a non-duplicate Stack Overflow question? You search before asking. Which we are all required to do always. Also when you tell us *but it seems not working*, please be precise and specific about how observed and expected results differ. Give an example in the question. – Ole V.V. Jan 15 '23 at 08:36

3 Answers3

1

Generate a list of numbers 1-20, then shuffle the list, then take the first 10.

List<Integer> nums = IntStream.rangeClosed(1, 20).boxed().collect(toList());
Collections.shuffle(nums);
nums = nums.subList(0, 10);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Your problem is in while(list[i] == list[i-1]) This isn't checking that the list doesn't contain the random number you generated. It's checking that the last 2 generated numbers aren't the same You're not checking that the previous numbers didn't match. The right way to do this is:

for (int i = 0; i < list.length; i++){
    bool found = false;
    do{
        random = (int)(Math.random() * 21);
         for(int j=0; j<i; j++) {
            if(list[j] == random) {
               found = true;
            }
        }
    }while(found = false);
    list[i] = random;
}
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0
public static void main(String[] args) {
    int[] myList = new int[10];

    number(myList);
}

public static void number(int[] list) {
    int random;
    Set<Integer> used = new HashSet<>();

    for (int i = 0; i < list.length; i++) {
        do {
            random = (int) (Math.random() * 21);
        } while (used.contains(random)); // check if the number has been used before

        list[i] = random;
        used.add(random);
    }
}
Andres Traumann
  • 144
  • 1
  • 8