Speedo
Speedo

Reputation: 55

Storing non-duplicated numbers in an array

Overview: My program has to store 20, non-duplicated randomly generated numbers from a range of 1 to 100 in an array. Problem: If I find a match(duplicated #) in the inner for loop, I flag the boolean variable. Here is what I don't know how to do. After the for loop, if the boolean hasn't been flagged, I want to add the random number to the array. I also want to increment x (until I have 20 non-duplicated numbers stored) but only if I add the element to the array.

public void duplication(){
    int max = 100; // max value for range
    int min = 1; // min value for range
    boolean duplicate = false;
    Random rand = new Random();

    for (int x = 0; x < 20; x++){
        //initiates array that stores 20 values
        int[] all = new int[20];
        //generates # from 1-100
        int randomNum = rand.nextInt((max - min) + 1) + min;
        all[x] = randomNum;
        //iterates through array
        for (int i : all) {
            //if there's a match (duplicate) flag boolean
            if (i == randomNum){
                duplicate = true;
            }
            else {
                duplicate = false;
            }
        }
    }
    //if boolean hasn't been flagged
    if (duplicate=false){
        //store to array
    }
}   

Upvotes: 2

Views: 1611

Answers (3)

Pubci
Pubci

Reputation: 4001

You can do this by using a while loop and a for loop as follows. In the while loop you generate a number and then you check for the duplication. If it is not a duplicate value, add to the array and increment the index value. If it is duplicate value, again while loop continues.

 public static void duplication() {
    int max = 100; // max value for range
    int min = 1; // min value for range
    Random rand = new Random();
    int index = 0;   // counter to track 20 numbers
    int[] all = new int[20];

    while (index < 20) {

        boolean duplicate = false;
        int randomNum = rand.nextInt((max - min) + 1) + min;

        for (int i = 0; i < all.length; i++) {
            //if there's a match (duplicate) flag boolean
            if (all[i] == randomNum) {
               duplicate = true;
               break;
            }
        }

        if (!duplicate) {
            all[index++] = randomNum;
        }
    }
}

Upvotes: 1

TheLostMind
TheLostMind

Reputation: 36304

This will give you an array of random integers between 0 to 100

public static void main(String[] args) {
    Set<Integer> s = new HashSet<Integer>(); // create a set
    Random r = new Random();
    while (s.size() < 20) {
        s.add(r.nextInt(100)+1);  // add elements to set upto size =20
    }
    Integer[] array = new Integer[20];
    s.toArray(array); // convert set to array of Integers
    System.out.println(Arrays.toString(array));

}

O/P :

[68, 69, 64, 65, 66, 67, 47, 74, 14, 17, 16, 21, 81, 54, 52, 82, 59, 57, 63, 88]

Upvotes: 5

JohnnyAW
JohnnyAW

Reputation: 2876

first you need to init your array before the loop, second, dont set the duplicate to false in the second loop, since you a number at the end can set it to false, even if there was a duplicate before. And the last one, set the value only if you didnt find the duplicate and decrese the x-value if duplicate is true.

public void duplication(){
    int max = 100; // max value for range
    int min = 1; // min value for range
    boolean duplicate = false;
    Random rand = new Random();

    //initiates array that stores 20 values
    //you need to init it before the loop!
    int[] all = new int[20];
    for (int x = 0; x < 20; x++){
        duplicate = false;
        //generates # from 1-100
        int randomNum = rand.nextInt((max - min) + 1) + min;
        //iterates through array
        for (int i : all) {
            //if there's a match (duplicate) flag boolean
            if (i == randomNum){
                duplicate = true;
                //we can break the loop here
                break;
            }
        }
        //if boolean is true, just stay at the same x value(for loop will increase by 1, thats why we decrese it by 1 here)
        if (duplicate){
            x--;
        } else {
            //if everything is ok, set the number
            all[x] = randomNum;
        }
    }
}   

Upvotes: 1

Related Questions