Thread: seed rand not correct values

  1. #1
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17

    seed rand not correct values

    int seed = 1111; // Static seed for predictability reasons
    srand( seed ); // Seed the random generator
    int ship_x = rand() % 10 + 1; // x in [1, 10]
    int ship_y = rand() % 10 + 1; // y in [1, 10]

    The program SHOULD be spitting out 5 and 5 for ship_x and ship_y. Although it used to work it is now giving me 7, and 8. I need to get this to work so that it will be graded properly. I will provide the whole program. It simulates the game Battleship.

    #include <stdio.h>
    #include <math.h>

    int main (void)
    {

    int seed = 1111; // Static seed for predictability reasons
    srand( seed ); // Seed the random generator
    int ship_x = rand() % 10 + 1; // x in [1, 10]
    int ship_y = rand() % 10 + 1; // y in [1, 10]


    int x,y;
    printf("Welcome to the Battleship Game\n");
    printf("Guess x coordinate: ",x);
    scanf("%d",&x);
    printf("Guess y coordinate: ",y);
    scanf("%d",&y);
    printf("\n");
    if(x==ship_x&&y==ship_y)
    {
    printf("HIT! Congratulations!\n");
    }
    else
    {
    int q,p;
    q=abs(x-ship_x);
    p=abs(y-ship_y);
    printf("MISS! You hit ");
    if(x-ship_x>0)
    {
    printf("%d blocks EAST",q);
    }
    if(x-ship_x<0)
    {
    printf("%d blocks WEST",q);
    }
    if(x!=ship_x&&y!=ship_y)
    {
    printf(" and ");
    }
    if(y-ship_y>0)
    {
    printf("%d blocks NORTH",p);
    }
    if(y-ship_y<0)
    {
    printf("%d blocks SOUTH",p);
    }
    }
    printf(" of the ship.");
    printf("\n");
    printf("The ship was located at (%d, %d).",ship_x,ship_y);
    system("PAUSE");
    return 0;
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    First up, welcome to the forums.

    As a matter of housekeeping, so to speak, the forum rules require that you use code tags around the code you post, in order to preserve indentation. You can read about that here: << !! Posting Code? Read this First !! >> You might want to look at some of the other board stickies while you are at, in terms of general information regarding posting in these forums.

    At any rate, here is your code with code tags:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    
        int seed = 1111;            // Static seed for predictability reasons 
        srand(seed);                // Seed the random generator 
        int ship_x = rand() % 10 + 1;   // x in [1, 10]
        int ship_y = rand() % 10 + 1;   // y in [1, 10]
    
    
        int x, y;
        printf("Welcome to the Battleship Game\n");
        printf("Guess x coordinate: ", x);
        scanf("%d", &x);
        printf("Guess y coordinate: ", y);
        scanf("%d", &y);
        printf("\n");
        if (x == ship_x && y == ship_y) {
            printf("HIT! Congratulations!\n");
        } else {
            int q, p;
            q = abs(x - ship_x);
            p = abs(y - ship_y);
            printf("MISS! You hit ");
            if (x - ship_x > 0) {
                printf("%d blocks EAST", q);
            }
            if (x - ship_x < 0) {
                printf("%d blocks WEST", q);
            }
            if (x != ship_x && y != ship_y) {
                printf(" and ");
            }
            if (y - ship_y > 0) {
                printf("%d blocks NORTH", p);
            }
            if (y - ship_y < 0) {
                printf("%d blocks SOUTH", p);
            }
        }
        printf(" of the ship.");
        printf("\n");
        printf("The ship was located at (%d, %d).", ship_x, ship_y);
        system("PAUSE");
        return 0;
    }
    You should do #include <stdlib.h> for rand(), srand(), and abs(). Also, you have some extra stuff in your printf() statements (in green, above). Have you got compiler warnings turned on? This would have told you all that I said here.
    Last edited by kermit; 09-28-2010 at 06:23 PM.

  3. #3
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    Thanks for fixing it, but no there are no warnings, and they are enabled. Those values are not mistaken for ship_x or ship_y, the x and y integers refer to what the user inputs, and ship_x or ship_y is the actual position of the battleship.

    I tried stdlib.h, no different.

    thanks for the try, any further suggestions?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If your compiler has warnings turned on, and did not bark at you for the problems with your code, then I would suggest you get a different compiler. What are you using for a compiler anyway?

    As far as the x and y arguments that you passed to printf(), they are extraneous. They do absolutely nothing but produce warnings.

    Can you give an example of what you expect the output of your program to be (exactly), and what the actual (exact) output is?
    Last edited by kermit; 09-28-2010 at 06:49 PM.

  5. #5
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    are you sure? haha, The way you posted it suggests theres a space there but there isn't.. i am using gcc built into dev c++ my outputs work perfectly fine but i need the first random value to output 5,5... because school's grading program is marking it wrong just in that case.

    some sample output would be: (where bold is just what the user is typing)

    Welcome to the Battleship Game
    Guess x coordinate: 3<enter>
    Guess y coordinate: 1<enter>
    MISS! You hit 2 blocks WEST and 4 blocks SOUTH of the ship.
    The ship was located at (5, 5).

    Welcome to the Battleship Game
    Guess x coordinate: 5<enter>
    Guess y coordinate: 5<enter>
    HIT! Congratulations!
    The ship was located at (5, 5).

    Welcome to the Battleship Game
    Guess x coordinate: 5<enter>
    Guess y coordinate: 8<enter>
    MISS! You hit 3 blocks NORTH of the ship.
    The ship was located at (5, 5).

    In these cases you see ship was located 5,5... thats what i want. but instead its giving me 7,8
    Last edited by bloodshot772; 09-28-2010 at 07:09 PM.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Nice. Your input/output (as originally posted) had absolutely nothing to do with the code you posted. Glad to see you got it fixed. Thought you were a bit of a scammer there for a minute or two.

    As for the printf(), yes I am absolutely sure. Perhaps you should read up on your documentation, and learn how it works for yourself. Also, I am using GCC, and have been using it for many years. I know this much, that if I don't use any warning switches, it will give me no warnings on the mistakes you have made. But if I use -Wall and -Wextra (might be -W for you, as I think your version of GCC will likely be older if you are using Dev C++) and I get the warnings. You are not using those warnings, which you _should_ always use (at a minimum).

  7. #7
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    Hehe... srry I copied the wrong text from the assignment... I'm a bit tired working on hmw.

    Thanks but I have to stick to what they teach us, do you think that could even have an impact on the srand statement? it seems to be a different factor.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I guess the point I was trying to make was that before you start trying to debug, crank up your compiler warnings, and fix everything it squawks about. There is not much sense in trying to debug until you do that much.

    Other than that, I actually don't know what to tell you. You should be getting the same numbers. I do happen to be getting 5,5 here/
    Last edited by kermit; 09-28-2010 at 08:32 PM.

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I'm pretty sure each implementation can give a different sequence for a given seed. Are you using the same compiler as your school?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    did it work just straight in like that? or did you fix it up. And if yes, how did you fix it?

  11. #11
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    Yes its gcc, it worked the first time around when I was still developing the program, and then it changed. I try using just the snippet to isolate and find the problem, but even at the root of that srand statement it outputs 7,8 now...

    I tried compiling using both dev c++ and cygwin, same output.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you only want 5, 5 for the ship's x and y values, then why use a rand() function?

    And if you want the same sequence of random numbers for testing, don't include the line about seeding the random number generator.

    Then you'll get the same sequence of numbers.
    Last edited by Adak; 09-28-2010 at 10:15 PM.

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by bloodshot772 View Post
    did it work just straight in like that? or did you fix it up. And if yes, how did you fix it?
    It seemed to work the way you expected with your posted code, as well as with fixing the headers, the calls to printf(), and declaring the variable seed as an unsigned int.

    Quote Originally Posted by Adak View Post
    If you only want 5, 5 for the ship's x and y values, then why are use a rand() function?

    And if you want the same sequence of random numbers for testing, don't include the line about seeding the random number generator.

    Then you'll get the same sequence of numbers.
    I am guessing that the OP will seed rand with srand(time) or such after getting the kinks worked out of the program. By sending the same value to srand() each time, he should be getting the same numbers.
    Last edited by kermit; 09-28-2010 at 08:57 PM.

  14. #14
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    So what should I do? nothing seems to work.. can you compile it and/or upload the .c or .exe here? To see if that will work, if not I will try doing it with another computer/program. My school has redhat, that might fix it. haha

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Here is my revised version. I do not have a .exe, because I run Linux. The source code is altered from your original in that I changed <math.h> to <stdlib.h> to account for rand(), srand(), and abs(). I also deleted the extra arguments to printf(), and deleted the call to system() (Which does not work that way on Linux). Finally, I changed the declaration of the variable seed to unsigned int.

    Maybe make a new project, and compile it and see what happens.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        unsigned int seed = 1111;            // Static seed for predictability reasons 
        srand(seed);                // Seed the random generator 
        int ship_x = rand() % 10 + 1;   // x in [1, 10]
        int ship_y = rand() % 10 + 1;   // y in [1, 10]
    
    
        int x, y;
        printf("Welcome to the Battleship Game\n");
        printf("Guess x coordinate: ");
        scanf("%d", &x);
        printf("Guess y coordinate: ");
        scanf("%d", &y);
        printf("\n");
        if (x == ship_x && y == ship_y) {
            printf("HIT! Congratulations!\n");
        } else {
            int q, p;
            q = abs(x - ship_x);
            p = abs(y - ship_y);
            printf("MISS! You hit ");
            if (x - ship_x > 0) {
                printf("%d blocks EAST", q);
            }
            if (x - ship_x < 0) {
                printf("%d blocks WEST", q);
            }
            if (x != ship_x && y != ship_y) {
                printf(" and ");
            }
            if (y - ship_y > 0) {
                printf("%d blocks NORTH", p);
            }
            if (y - ship_y < 0) {
                printf("%d blocks SOUTH", p);
            }
        }
        printf(" of the ship.");
        printf("\n");
        printf("The ship was located at (%d, %d).", ship_x, ship_y);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 47
    Last Post: 07-13-2010, 07:22 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  4. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM