传输中随机数发生器输出丢失 - C.

时间:2015-02-04 05:19:12

标签: c

我的任务是编写一个与生日问题类似的程序。基本上,如果一个房间里有50个人,有可能有一个0-99之间的数字,那么获得重复数字需要多少人。计算1000次后(计算应该发生在main()之外的单独函数中),我需要在直方图中将结果打印到屏幕上。

我目前唯一的问题是,在某个地方我似乎只拾取了一个在collider()中的随机数生成器中生成的数字,并将其应用于我试图计算的整个数组命中显示在我的直方图中。我似乎无法弄清楚这发生了什么。任何帮助将不胜感激。

#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

0 个答案:

没有答案