1

I've been tasked with writing a program similar in nature to the birthday problem. Essentially, given 50 people in a room, who have the possibility of having a number between 0-99, how many people does it take to get a repeat number. After calculating that 1000 times (the calculation should happen in a separate function outside of main()), I need to print the results to the screen in a histogram.

My only issue at the moment is that somewhere along the way I seem to pick up only one number that was generated in the random number generator in collider(), and apply that to the entire array in which I was trying to count the hits to display in my histogram. I can't seem to figure out at what point this happens. Any help would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
#define N 50

int collider();

int main()
{
//Sample will be the number of times collider is called
int Sample = 1000;
int Z;
int W;
int Q;
int res[Sample];
int hist[N];

//Call collider() Sample amount of times and
//populate an int array with the results
for(Z = 0; Z < Sample; Z++)
    {
        collider();
        res[Z] = collider();
        for(W = 0; W < N; W++)
            {
                if (hist[W] = collider())
                    {
                        hist[W]++;
                    }
            }
    }

for(Q = 0; Q < N; Q++)
    {
        printf("%d: %f\n", (Q+1),(hist[Q]/1000.0));
    }

return 0;
}

int collider()
{
//define the test pool(N - people in the birthday problem) 
//and possible unique attributes(M - birthdays in the birthday problem)
int M = 100;
int i;
int x = 0;
int y = x + 1;
int arr[N];
time_t T = time(NULL);
srand(T);

//use for loop to populate an array of length N with numbers ranging 
//from 0 to 99
for(i = 0; i < N; i++)
    {
        arr[i] = rand() % (M-1);
    }

//iterate through the array at each position, comparing it to all other
//positions in the array.  if array[x] does not equal array[y] return 
//the result of y(the index of the first repeat) so it can be used by
//main

for(x = 0; x < N; x++)
    {
        for(y = x+1; y < N; y++)
            {
                if(arr[x] != arr[y])
                    {

                    }
                else
                    {
                        //jump out of the loop....I know....
                        goto stop;
                    }   
            }
    }

stop:
    //return the index number of the first repeat
    return y;
}//end main
ConeBaron
  • 21
  • 1
  • Is it possible that even though I seeded the random number generator – ConeBaron Feb 04 '15 at 05:22
  • You call `collider` twice for each iteration of the loop. You should call `srand` once at the beginning of your program, not inside `collider`. You're probably generating the same results every time because of that. – Retired Ninja Feb 04 '15 at 05:24
  • You seem to be calling `srand()` far too often. See: [`srand()` — why call it only once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) – Jonathan Leffler Feb 04 '15 at 05:27
  • I just made both changes you suggested and that appears to have solved the issue. Thanks for the help!!! – ConeBaron Feb 04 '15 at 05:28

0 Answers0