not selecting random number again

This is a discussion on not selecting random number again within the C++ Programming forums, part of the General Programming Boards category; I'm working on the game battleship on c++ and everything works out great except for the part that its the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    not selecting random number again

    I'm working on the game battleship on c++ and everything works out great except for the part that its the AI turn to take a shot. I got it to work with the AI picking a random number, but it sometimes picks the same number twice. so I was working on the code to check if the coordinates had already been picked before. according to me the code below should work, but if doesn't it stills picks the same coordinates twice.

    What its supposed to do is pick to random numbers. then it checks it with the other previous coordinates. if they match it sets attack to false and the while loop repeats. then it stores the coordinates in a vector.
    this is a member function of a class. Row and column are declared private in the class.

    Code:
    vector<int> prevAttacksY(1, -1);
        vector<int> prevAttacksX(1, -1);
        bool attack;
        int count = 0;
        while(!attack){
            row = rand() % 8;
            column = rand() % 8;
            attack = true;
            for(count = 0; count < prevAttacksY.size(); count++){
                    if (row == prevAttacksY.at(count) && column == prevAttacksX.at(count))
                        attack = false;
            }
        }
        prevAttacksY.push_back(row);
        prevAttacksX.push_back(column);
    Last edited by diego; 05-22-2010 at 05:05 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by diego View Post
    picking a random number, but it sometimes picks the same number twice.
    Well, that is possibility of random numbers. Here's a tip about rand: if you are going to use it, seed the RNG when the program first starts with srand(time(NULL)). Only do that *once*. If you call srand() repeatedly, you may end up with the same series of numbers repeating.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    That's not the problem. Calling srand() will give you a difference sequence of random numbers each time you start the program. What the OP is trying to do here is prevent the same 2D coordinate from being generated multiple times.

    You never set attack to false to begin with. The code might run right now, because in some debug builds variables are initialized to zero, but it's undefined behaviour. Your while loop might never get entered.

    Assuming your board has a fixed size, it would probably be way easier and certainly more efficient -- O(1) instead of O(n) -- to use something like this.
    Code:
    bool picked[WIDTH][HEIGHT] = {};  // automatically init to all false
    
    int x, y;
    do {
        x = rand() % WIDTH;
        y = rand() % HEIGHT;
    } while(picked[x][y]);
    picked[x][y] = true;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by dwks View Post
    That's not the problem. Calling srand() will give you a difference sequence of random numbers each time you start the program. What the OP is trying to do here is prevent the same 2D coordinate from being generated multiple times.

    You never set attack to false to begin with. The code might run right now, because in some debug builds variables are initialized to zero, but it's undefined behaviour. Your while loop might never get entered.

    Assuming your board has a fixed size, it would probably be way easier and certainly more efficient -- O(1) instead of O(n) -- to use something like this.
    Code:
    bool picked[WIDTH][HEIGHT] = {};  // automatically init to all false
    
    int x, y;
    do {
        x = rand() % WIDTH;
        y = rand() % HEIGHT;
    } while(picked[x][y]);
    picked[x][y] = true;
    not setting attack to false was a big mistake. your code makes perfect sense and is much better but I tried it and it is still giving repeating coordinates.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Hmm, it shouldn't be. Did you make the picked[][] array static or global? If you just declared it as a local array it would be reset to all false every time the picking function was executed.

    If it's still not working, you may want to post some of your code so that we can look at it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by dwks View Post
    Hmm, it shouldn't be. Did you make the picked[][] array static or global? If you just declared it as a local array it would be reset to all false every time the picking function was executed.

    If it's still not working, you may want to post some of your code so that we can look at it.
    that got it working. I had it declared in the function. I changed it to global and it worked. thanks a lot. I couldn't figure out the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  3. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 07:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21