Skip to content
Advertisement

Generating an array of non-repeating integers

I’m trying to generate an array of 5 non-repeating integers in Java, but there are still repeats when I run it. Here’s my code so far:

public int[] generateCode(){
    code[0] = (int)Math.round(Math.random()*8+1); // initialize first number so it won't be compared against 
    for(int i=1; i<code.length; i++){
        code[i] = (int)Math.round(Math.random()*8)+1;
        for(int j=0; j<i; j++){
            while(code[i]==code[j]){
                code[i] = (int)Math.round(Math.random()*8)+1;
            }
        } // end inner for loop
        
    } // end outer for loop
    return code;
    
    
} // end generateCode method

Any help is very much appreciated!

Advertisement

Answer

So, your for-loop is checking for repeated characters, BUT each time you generate a new value (in the while-loop) you’re not checking to see if the value exits before j.

There are a number of possible ways you might address this issue, I prefer ones which uses Collections.shuffle, but assuming that you can’t use features like Arrays, Collections, List, Set or possibly even streams, we need to work with what we have, arrays.

Since you have a small range of acceptable values which need to fit into an even smaller range, a simple solution might be to generate a “master” list of allowed values and randomly select a value from the master list, tracking which values you’ve already picked.

Sounds more complicated then it actually is, for example…

public int[] generateCode() {
    int[] masterValues = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int[] codes = new int[5];
    
    Random rnd = new Random();
    int index = 0;
    while (index < codes.length) {
        int lookupIndex = 0;
        do {
            lookupIndex = rnd.nextInt(masterValues.length);
        } while (masterValues[lookupIndex] == 0);
        codes[index] = masterValues[lookupIndex];
        masterValues[lookupIndex] = 0;
        index++;
    }
    return codes;
}

This creates a “master” list of values. It then randomly calculates a “lookup” index, checks to see if the value in the master list is 0 or not, if not, it assigns it to the next index in the codes array and sets the value in the master list to 0, otherwise it generates a new random index and tries again. This all repeats till it fills the codes array

So doing something like…

System.out.println(Arrays.toString(generateCode()));
System.out.println(Arrays.toString(generateCode()));
System.out.println(Arrays.toString(generateCode()));
System.out.println(Arrays.toString(generateCode()));

could print (because it’s “random”)…

[8, 1, 4, 7, 5]
[9, 6, 2, 1, 8]
[6, 5, 9, 4, 7]
[2, 5, 3, 1, 4]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement