为什么int上的return值是一样的呢?

Why is the return value on int is the same over?

S,我正在创建一些基于随机值的游戏。这个游戏应该通过插入一个球队和另一支球队的球员统计数据来预测足球比赛的结果,等等。增加比赛数据的球队将增加对手的数据。球员的原始数据会除以2放在概率条的边缘,如果该值触及球队的数据会考虑得分,但如果在中间则为平局。

这是代码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>

int score_prob(int x,int hh, int aw);

int main()
{
    int i,j,k,a,b,c,n,sc,shome=0,saway=0;
    int p[64][64],x,y,z,ho,aw;
    int t[64],h,w,sem,tand;

    printf("Number Of Team  : ");scanf("%d",&x);
    printf("Number Of Player: ");scanf("%d",&y);

    for(i=0;i<x;i++)
    {
        z = 0;
        printf("\n\n");
        printf("Teamm %d \n",i+1);
        for(j=0;j<y;j++)
        {
            printf(". PLayer %d : " ,(j+1));scanf("%d",&p[i][j]);
            z += p[i][j];
        }
        printf("Stat of team %d = %d",i+1,z);
        t[i] = z;
    }
    srand(time(NULL));

    do
    {
        k = 0;
        printf("\n\n");
        printf("Home Team Number: ");scanf("%d",&a);a = a - 1;
        printf("Away Team Number: ");scanf("%d",&b);b = b - 1;
        for(i=0;i<90;i++)
        {
            i += 4;
            printf("\nMinute %d: ",i+1);
            {
                sem  = t[a] + t[b];
                ho=t[a];aw=t[b];

                sc = score_prob(sem,ho,aw);
                printf("\n              %d\n",score_prob(sem,t[a],t[b]));
                if(sc == 0)
                {
                    shome += 1;
                    printf("Home Score\n");printf(" %d VS %d ",shome,saway);
                }else if(sc == 2)
                {
                    printf(" %d VS %d ",shome,saway);
                }
                else{
                    saway += 1;
                    printf("Away Score\n");printf(" %d VS %d ",shome,saway);
                }
            }
        }
    }while(k == 0);
    return 0;
}

int score_prob(int x,int hh, int aw)
{
   int st,aa,nn,sr=0,sh=0,sa=0,home,away;
   time_t t;
   sh=0;sa=0;
   aw = aw / 2; hh = hh / 2;
   srand((unsigned) time(&t));

   for(nn=0;nn<x;nn++)
   {
        aa = rand() % x;
        if(aa<hh)
        {
            sh += 1;
        }
        if(aa>(aa - aw))
        {
            sa += 1;
        }
        if(aa!=(aa<hh) && aa != (aa<(aa-aw)))
        {
            sr +=1;
        }
   }

   if(sr != 0)
   {
       if(sh<sa)
       {
           st = 1;
       }
       else
       {
           st = 0;
       }
   }
   else
   {
       st = 2;
   }

   return st;
}

rand 是 'secretly' 递归函数(属于 Linear congruential generator 类型)。这意味着上一次调用的结果用作下一次调用的输入。由于它将先前的值存储在静态变量中,因此您需要在第一次调用 rand 之前自行设置它。

您可以在运行时多次重置它,但是由于您可能在同一秒内多次调用 srand((unsigned) time(&t));,因此该值似乎是恒定的。

随机数不是很随机 =) 有一些 table 带有伪随机数,当您调用 srand() 时,您只是 select 其中一个 tables。之后,对 rand() 的每次调用都会从 selected table 中获取一些号码。因此,如果您使用相同的种子多次调用 srand(),您将一遍又一遍地获得相同的 table。