HELP ME!

+1 Ray Haq · December 6, 2014

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

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!

Post a Reply

Replies

Oldest  Newest  Rating
0 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 <stdlib.h>
#include <stdio.h>
#include <time.h>

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... */
+1 Ray Haq · December 6, 2014
thanks man!!
help me to make it more efficient.

#include <stdio.h>
#include <stdlib.h>

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?
0 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 circumstances

first error:
/images/forum/upload/2014-12-07/e9898e23682124b3dd3bd085e89bedc4.png
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:
/images/forum/upload/2014-12-07/f0e09ecae9997b4cae7afdc8cb0bc70b.png
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:
/images/forum/upload/2014-12-07/ef30e32e5e0ed47c87a899cfc90ee3a8.png
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:
/images/forum/upload/2014-12-07/b1a83712ec2463d5beaea592cd34fa94.png
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! :P
0 Jan Meier · December 7, 2014
I study game development now for my new position and your thread helped me out greatly!
+1 Ray Haq · December 8, 2014
Thanks Man!! i really appreciate that!! :) #c student
0 c student · December 8, 2014
no problem!  remember, testing is just as important as making the program! 

8-)

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 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....
0 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:
/images/forum/upload/2014-12-09/d29b8170cc328f1aa380f87712b54cef.png

when i entered in the negative values:
/images/forum/upload/2014-12-09/3be1fac9b8426894c06ba13996ab6371.png
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:
/images/forum/upload/2014-12-09/fcc7690920a64faa6c6ff9db7d6f3130.png

hope you understand now! :D
+1 Ray Haq · December 9, 2014
:) oh yeah!!! i got it!!!
what can i use to solve this??? absolute value??
+1 c student · December 9, 2014
well that's up to you to decide!  :woot:  there may be many different solutions out there, try and find one that works for you.

good luck!  8-)
  • 1

C

103,540 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator