0

S, i was creating some game based on random value. This game should predict how the soccer game will turn out by inserting the stat of the player for one team combined and the other combined and so on. The team that put on game stat will add by the rival stat. The original stat of the player would be divided by two and put in the edge of the probability bar if the value touch the stat of the team it will consider score but if it's in the middle it would become draw.

Here's the code

#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;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
KotjengOren
  • 79
  • 1
  • 7
  • 8
    Don't call `srand` more than once. – David Schwartz Apr 28 '17 at 11:02
  • @luoluo Because if you call it with the same parameter you get the same random numbers - that's part of the whole point of srand. And if your program takes less than 1 second, then every time you call `time` you get the same number (probably). – user253751 Apr 28 '17 at 11:12

2 Answers2

2

Random numbers are not quite random =) There are some tables with pseudo-random numbers and when you call srand() you just select one of those tables. After that every call for rand() will take some number from the selected table. So if you call srand() several times with the same seed you'll get the same table over and over again.

Max Pashkov
  • 404
  • 1
  • 3
  • 12
  • 2
    It's probably more accurate to say there's one big table, and `srand` sets the current position in that table, and `rand` returns the next number in the table (advancing the position as it does). It's even more accurate to say there's a function that would generate each row of the table, one-by-one, based on the current value. `srand` sets the current value and `rand` calculates the next number (updating the current value). – TripeHound Apr 28 '17 at 11:28
  • @TripeHound yes, thanks for pointing out =) – Max Pashkov Apr 28 '17 at 11:35
1

rand is 'secretly' a recursive function (of the Linear congruential generator kind). It means the result of the previous call is used as the input for the next call. As it stores this previous value in a static variable, you need to set it yourself before calling rand for the first time.

You can reset it during runtime multiple times, however as you probably call srand((unsigned) time(&t)); multiple times within the same second, this value seems constant.

Lanting
  • 3,060
  • 12
  • 28