【发布时间】:2017-04-28 11:01:05
【问题描述】:
S,我正在创建一些基于随机值的游戏。这个游戏应该通过插入一个球队的球员数据和另一支球队的球员数据来预测足球比赛的结果,依此类推。加入比赛数据的球队将添加对手数据。球员的原始数据将被除以二,如果数值触及球队的数据将考虑得分,但如果它在中间,则将其置于概率栏的边缘。
这是代码
#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;
}
【问题讨论】:
-
不要多次致电
srand。 -
@luoluo 因为如果你用相同的参数调用它,你会得到相同的随机数——这是 srand 的一部分。如果您的程序花费的时间少于 1 秒,那么您每次调用
time时都会得到相同的数字(可能)。