# HELP ME!

 Ray Haq · December 6, 2014 ``#include #include #include #include #include int main(){    int i;    int diceroll;    int total1=0;    int total2=0;    char guess;    for(i=0; i < 3; i++){    diceroll = (rand()%6 ) + 1;    printf("%d \t", diceroll);    total1 = total1 + diceroll; }    printf("\nAnd the total is\t %d", total1);    printf("\nNext round will be Higher, Lower or Same? (h/l/s) ");    scanf(" %c", &guess);    return 0;}``Why the random numbers are same everytime??? what should i do? someone plz help!

## Replies

 Ray Haq · December 9, 2014  oh yeah!!! i got it!!!what can i use to solve this??? absolute value?? c student · December 9, 2014 well that's up to you to decide!    there may be many different solutions out there, try and find one that works for you.good luck! Ray Haq · December 6, 2014 thanks man!!help me to make it more efficient.``#include #include int main(){   int i;   int players[3];   int goals[3];   int gamesPlayed[3];   float gpg[3];   float bestgpg = 0.0;   int bestplayer; /*Goals per game*/   for(i=0; i< 3; i++){       printf("Enter the players jersy one by one.\n");       scanf(" %d\n%d\n%d", &players[0], &players[1], &players[2]);   break;   }   for(i=0; i< 3; i++){        printf("Enter the goals scored: \n");       scanf(" %d\n%d\n%d", &goals[0], &goals[1], &goals[2]);   break;   }   for(i=0; i< 3; i++){       printf("Enter the gamesplayed: \n");       scanf(" %d\n%d\n%d", &gamesPlayed[0], &gamesPlayed[1], &gamesPlayed[2]);   break;   }   printf("\t\t\tStats:\n");   printf("Player\tGames Played\tGoals Scored\tGoals per Game \n");   for(i=0; i< 3; i++){       gpg = (float)goals / (float)gamesPlayed;       printf(" %d\t\t%d\t\t%d\t\t%.2f \n", players, gamesPlayed, goals, gpg);   }   for(i=0; i< 3; i++){       if(gpg > bestgpg){           bestgpg=gpg;           bestplayer=players;       }   }   printf("\n%d IS THE BEST PLAYER\n", bestplayer);       return 0;}``this was buckys tutorial..... i wanted to do it a bit differently.it works but i want to improve it..... was the scanf section right? or theres other way to do it? Ray Haq · December 8, 2014 Thanks Man!! i really appreciate that!!  #c student Ray Haq · December 8, 2014 that has gone over my head bro!!!but i'll try my best! i've known the problems........ & thats the 1st thing to do!thanks again!! really appreciate your posts.... c student · December 9, 2014 when you first declare a variable:``   int bestplayer;``some memory will be allocated for it, in this case, it will be allocated 4 bytes of space since an int holds the size of 4 bytes.  when that memory is given to it, the variable will have whatever value was already inside that memory space.  in my specific case, it was 32767:when i entered in the negative values:your goals variables were all set to -1, meaning your gpg value became -1 after the following calculation:``       gpg = (float)goals / (float)gamesPlayed; // gpg value is now -1``when i arrived at your loop which was supposed to change your bestplayer variable:``       if(gpg > bestgpg){ // gpg variable is -1, so gpg < bestgpg; loop condition failed           bestgpg=gpg;           bestplayer=players; // value not changed       }`` the loop condition failed and so the value was never changed, meaning it still held the initial value of 32767, leading to this output:hope you understand now! Susan Moss · December 6, 2014 You need to initialize the random seed each time you generate a (pseudo) random number.Try using:srand(time(NULL));Exaple:``#include #include #include void main( void ){   int i;   /* Seed the random-number generator with current time so that    * numbers will be different every time we run...    */   srand( time( NULL ) );   /* Display 3 numbers. */   for( i = 0; i < 3; i++ )      printf( "%d\t", rand()%6 +1 );}/* rest of the code here... */`` c student · December 7, 2014 hi there, nice program you've made!  although it might seem fine, i have tested your code and found a few errors in extraordinary circumstancesfirst error:there is no best player if the scores are all equal (that's favoritism!).  you'll need to make an exception for this case!second error:i want to point out the obvious that 32767 is not a valid result! who is 32767 exactly and what's this "-nan"? he's the maximum number an int can hold and you have received this value due to the following error. nan represents an undefined number, that is, in basic mathematics, you cannot divide any number by 0 (not even 0 itself!):``       gpg = (float)goals / (float)gamesPlayed;   // cannot divide by 0``make sure you add an exception for this case too!third error:gah! the phantom player 32767 has appeared... again!  i actually don't know the specific reason for this error but i'm guessing that it has something to do with negative int to float conversion(?). here it is again:the first goal scored value has overflown one of your buffers and spilled into your other variables.  in that red circle, i didn't even get a chance to type in the games played!make sure you check for valid inputs (equal to or greater than 0?)!you shouldn't really need to worry about these errors except for the first if this is a small-time project, especially if someone intends to use your program correctly.  but better to be safe than sorry and get it right now, right?good luck! Jan Meier · December 7, 2014 I study game development now for my new position and your thread helped me out greatly! c student · December 8, 2014 no problem!  remember, testing is just as important as making the program! p.s. the negative number errors were most probably caused by the fact that gpg was always less than bestgpg thus never entering the loop:``       if(gpg > bestgpg) { // gpg < bestgpg           bestgpg=gpg;            bestplayer=players; // bestplayer value not changed       }``when you declared the variable:``int bestplayer; // default initialization; garbage value``it was assigned the value of whatever was previously there by default.so in combination, bestplayer was given a garbage value from declaration and was never changed.
• 1

## C

124,517 followers