0

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. (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);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
sarah
  • 59
  • 1
  • 5
  • 2
    `srand(time(NULL));` should be called only once at the start to initialized the RNG. – Osiris Jan 20 '19 at 23:03
  • @Osiris I don't know what you mean. I only have that in the code once. – sarah Jan 20 '19 at 23:05
  • But you call `srand` everytime you generate a random number with `randNums`. You should remove `srand` from `randNums` and place it in `main`. – Osiris Jan 20 '19 at 23:06
  • @Osiris I put srand in main. Did I put it in the correct spot? – sarah Jan 20 '19 at 23:08
  • Please don't correct the mistake *in the post*, only in your own code. People won't understand what you were asking. Rolled back. Now it can be seen that you call `srand()` right before you call `rand()` every time. – Weather Vane Jan 20 '19 at 23:09
  • It should be at the beginning of `main` as initialization of the RNG. Since I do not see your `main` function, i can't tell. – Osiris Jan 20 '19 at 23:09
  • Also note that at `temp = randNums(1,50); temp = randArr[k];` you immediatly reassign `temp` so the first assignment has (nearly) no effect. – Osiris Jan 20 '19 at 23:12
  • @Osiris that makes sense. that was dumb on my part. I fixed it in my code on my laptop by saying temp = randNums(1,50); randArr[k + 1] = temp; but I am not getting anything to print out in the array. – sarah Jan 20 '19 at 23:17

2 Answers2

2

Several observations:

  1. You need to call srand() (which you're doing)

  2. You should call srand() only ONCE (you're calling it multiple times)

  3. Values returned by rand() can repeat. You need to account for that.

  4. Bear in mind that the C library uses a "pseudo-random" algorithm - it isn't truly "random".

Also look here:

https://www.gnu.org/software/libc/manual/html_node/Pseudo_002dRandom-Numbers.html#Pseudo_002dRandom-Numbers

The numbers generated are not truly random; typically, they form a sequence that repeats periodically, with a period so large that you can ignore it for ordinary purposes. The random number generator works by remembering a seed value which it uses to compute the next random number and also to compute a new seed.

...

You can obtain repeatable sequences of numbers on a particular machine type by specifying the same initial seed value for the random number generator. There is no standard meaning for a particular seed value; the same seed, used in different C libraries or on different CPU types, will give you different random numbers.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

Every call to randNums calls srand with the current system time as an argument.

The rand function does something along these lines (pseudocode):

rand()
{
     seed = (some permutation on seed);
     return seed%RAND_MAX;
}

srand changes the state of seed, which is accessed by rand, to whatever you passed as your argument. If your calls to randNums occur within the same second, they will both change the seed to the same value, so rand has the same seed in both cases and produces the same result.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85