Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an array containing unique Random ints, check for duplicates

Tags:

java

arrays

nodes

This is an assignment for school that I am really close to completing, but I don't have it exactly. I am supposed to generate an array of integer Nodes with 100 random numbers without duplicates by checking for duplicates. I'm not allowed to use a Set. I'm not allowed to just shuffle an array of numbers 1-1000.

Here is the code I have in my client class so far but it still creates duplicates:

for (int i = 0; i < SIZE; i++) {
    int j = (int)(Math.random()*1999);

 //put a random number in the first index.
    if (i == 0) {
        Node newNode = new Node(j);
        nodeArray[i] = newNode;
    }

    for (int k = 0; k < i; k++) {
        if (nodeArray[k].getInt() == j) {
            j = (int)(Math.random()*1999);
            break;
        } else {
            Node newNode = new Node(j);
            nodeArray[i] = newNode;
        }
    }
}
like image 491
Maggie S. Avatar asked Feb 11 '26 03:02

Maggie S.


2 Answers

The way i would do this would be to use a List to store all the random numbers in. Then when you generate a number you can check if it already exists in the list, and get another number (this is done via recursion, or a while loop). You keep going with this until your list is full. Then go through the list creating the Nodes for your array.

List<Integer> numbers = new ArrayList<Integer>(SIZE);
for (int i = 0;i<SIZE; i++) {
    addUniqueRandomNumber(numbers, SIZE*2);
}

for (int i =0;i<numbers.size; i++) {
  Node newNode = new Node(numbers.get(i));        
  nodeArray[i] = newNode;
}

The addUniqueRandomNumber method:

public static void addUniqueRandomNumber(List<Integer> numbers, int range) {
    int j = (int)(Math.random()*range);
    if (numbers.contains(j)) {
        addUniqueRandomNumber(numbers, range);
    } else {
        numbers.add(j);
    }
}
like image 50
M21B8 Avatar answered Feb 14 '26 18:02

M21B8


Because when you are assigning a new random if the first random is a duplicate in your second if statement it never checks if that random could be also a duplicate. You need to redo the loop and check if that number is a duplicate as well.

for (int k = 0; k < i; k++) {
    if (nodeArray[k].getInt() == j) {
        j = (int)(Math.random()*1999); //You must check if this random is also a dup
        break;
    } else {
        Node newNode = new Node(j);
        nodeArray[i] = newNode;
    }

Here's what I would do:

int i = 0;
while (i < SIZE) {
int j = (int)(Math.random()*1999);

 //put a random number in the first index.
if (i == 0) {
    Node newNode = new Node(j);
    nodeArray[i] = newNode;
    i++;
}

for (int k = 0; k < i; k++) {
    if (nodeArray[k].getInt() == j) {
        //Do nothing
    } else {
        Node newNode = new Node(j);
        nodeArray[i] = newNode;
        i++;
    }
  }
}

Basically only increment i if the number is not duplicate, otherwise go through and try to find another random that is not a duplicate.

like image 29
jrowe08 Avatar answered Feb 14 '26 18:02

jrowe08



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!