mercredi 27 février 2019

C trying to fix infinite loop

I have a function that gets an index value, puts it in an array. Then generates a new new random index using rand + srand(key). And it checks if the newly generated index is in array already, it will will keep on generating new index and checking until a unique value is generated.

The problem is that it works fine for small keys, but at longer keys it gets stuck in an infinite loop and can never find a unique value. Here's my code:

int getNewIndex(PPM *im, int index, int *visitedPixels, int *visitedPixelsIndex) {

    int i = 0;

    visitedPixels[*visitedPixelsIndex] = index;
    (*visitedPixelsIndex)++;
    // If index is already in the list, generate a new number and check again.
    while (i < *visitedPixelsIndex) {
        (index == visitedPixels[i]) ? index = rand() % im->height, i = 0 : i++;
    }

    return index;
}

EDIT: im->height which is the image height is about 400-600 in average.




Aucun commentaire:

Enregistrer un commentaire