dimanche 20 janvier 2019

I need help trying to figure out why the random number generator keeps repeating the same number?

The goal of this program is to add 6 random numbers into an array between 1 and 50 and there cant be any repeats. I am mostly having problems with checking to make sure that there aren't any repeats. I have tried multiple things and I just can't seem to figure it out. Please help. Thanks in advance!

(there is more code before this that doesn't relate to my problem)
..... 
 //puts random numbers into an array
            i = 0, j = 0;
            for (i = 0; i < arrSize; i++)
            {
                    randArr[i] = randNums(1,50);
            }


            //checks to make sure there are no duplicates
            i = 0, j = 0, k = 0, temp = 0;
            for (i = 0; i < arrSize; i++)
            {
                    for (j = 1; j <= arrSize;)
                    {
                            if (randArr[j] == randArr[i])
                            {
                                    for (k = j; k <= arrSize; k++)
                                    {
                                            temp = randNums(1,50);
                                            temp = randArr[k];
                                            randArr[k] = randArr[k + 1];
                                            randArr[k + 1] = temp;
                                    }
                            arrSize--;
                            }
                            else
                            j++;
                    }
            }
.....
//generates random numbers between the inputed max and min
int randNums(int min, int max)
{
        int result = 0, low = 0, high = 0;
        if (min < max)
        {
                low = min;
                high = max + 1;
        }
        else
       {
            low = max + 1;
            high = min;
       }

       srand(time(NULL));
       result = (rand() % (high - low)) + low;

       return (result);
}




Aucun commentaire:

Enregistrer un commentaire