2 baidu 34031378 baidu_34031378 于 2016.03.11 13:50 提问

C语言关于随机数生成时间和与中间数的比较

#include
#include
#include

#define NCALLS 1000
#define NCOLS 8
#define NLINES 3

int main(void)
{
int i, val;
long begin, diff, end;
double median = RAND_MAX / 2, above_cnt=0, below_cnt=0, differences;

begin = time(NULL);
srand(time(NULL));
for (i = 1; i <=NCALLS; ++i) {
    val = rand();
    if (val < median) 
        below_cnt += 1;
    else if (val>median)
        above_cnt += 1;
    if (i <= NCOLS*NLINES) {
        printf("%7d", val);
        if (i%NCOLS == 0)
            printf("\n");
    }
    else if (i == NCOLS*NLINES + 1)
        printf("%7s\n\n", ".....");
}
end = time(NULL);
diff = end - begin;
differences = above_cnt - below_cnt;
printf("%s%ld\n%s%ld\n%s%ld\n%s%.10f\n%s%ld\n\n",
    "           end time:", end,
    "         begin time:", begin,
    "       elapsed time:", diff,
    " time for each call:", (double)diff / NCALLS,
    "        differences:",differences);

scanf_s("%d", &i);
return 0;

}

1个回答

wen_wxpk
wen_wxpk   2016.03.11 14:27

问题是啥?。。。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!