0

So I got this code online, but it doesn't give a random number every time you call it. I'm trying to make noughts and crosses (agains't a bot) and this will be the code to let the bot decide where to place it's symbol, but every time I call it in an if statement (if (!playerTurn)) it gives the same number it gave the time before. This is the code I got so far.

 {
        srand(time(NULL));
        int rand = std::rand() % 8;

        cout << "Computer heeft geplaatst op positie " << rand << "\n";
        printBoard();
        playerTurn = true;

        
    }
  • Please, provide a [mcve]. `srand()` should be called only once in your program (e.g. in `main()`). Calling it every time before calling `rand()` makes the latter rather non-random. – Scheff's Cat Mar 05 '21 at 16:43
  • So I'm pretty new to c++, what would be the code for that? – Larsvd05 Mar 05 '21 at 16:44

0 Answers0