mardi 12 juillet 2016

I need to determine if a random number is unique

I need to write a function that receives the number generated in fillArray and determine if it has already been generated. I need to return a true or false thus determining if the random number must be put into the array. Here's what I am working on. Thanks for any help. I've searched for anything similar but unfortunately cannot find anything.

public void fillArray() {
    int randNum = (int) (Math.random() * 49) + 1;
    for (int i = 0; i < size; i++) {

        Arr[i] = randNum;
        alreadyThere(randNum);


    }
    size++;
}

public int alreadyThere(int randNum) {
    int find = randNum;
    boolean found = false;
    int i = 0;
    while (!found && i < size) {
        if (Arr[i] == find) {
            found = true;
        }
        i++;
    }
    if (!found) {
    }
    return randNum;
}




Aucun commentaire:

Enregistrer un commentaire