rand() help

I am trying to make a tic tac toe game with c++ console, and for a player vs. computer i am just going to make the computer place an 'O' in random positions. using a char array, each char in the array is a different area on the tic tac toe board. My problem is that sometimes the rand() function does not work, or just sits there. How do i fix it? (code is unfinished btw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void cpuGame()
{
    int input;
    char grid[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
    bool gameStarted = true;
    int cpuPiece;
    char player;
    bool playerTurn = false;
    cout << endl
         << " Gameboard                      Template "<< endl;
    cout << "   |   |                       7 | 8 | 9 " << endl
         << "---+---+---                   ---+---+---" << endl
         << "   |   |                       4 | 5 | 6 " << endl
         << "---+---+---                   ---+---+---" << endl
         << "   |   |                       1 | 2 | 3 " << endl
         << "\n\nWhere would you like to place your (X) Piece?\n(X):";
    cin >> input;
    input = input - 1;
    grid[input] = 'X';
    srand (time(NULL));
    while(gameStarted == true)
    {
        for(int i=0;i<15;i++)
        {
            cout << endl;
        }
        cout << endl << endl
             << " Gameboard                      Template "<< endl;
        cout << " "<<grid[6]<<" | "<<grid[7]<<" | "<<grid[8]<<"                     7 | 8 | 9 " << endl
             << "---+---+---                   ---+---+---" << endl
             << " "<<grid[3]<<" | "<<grid[4]<<" | "<<grid[5]<<"                     4 | 5 | 6 " << endl
             << "---+---+---                   ---+---+---" << endl
             << " "<<grid[0]<<" | "<<grid[1]<<" | "<<grid[2]<<"                     1 | 2 | 3 " << endl << endl;
        if(playerTurn == false){
        cout << "\nThinking...\n";
        for(;;){
        cpuPiece = rand() % 10;
        if(grid[cpuPiece] == 'O' || grid[cpuPiece] == 'X')
        {
            continue;
        }
        else
        {
            grid[cpuPiece] = 'O';
            playerTurn = true;
            break;
        }
        }
        }
        else
        {
            cout << "\n\nWhere would you like to place your (X) Piece?\n(X):";
            cin >> input;
            input = input - 1;
            grid[input] = 'X';
            playerTurn = false;
        }
    }

}
rand() must be initialised by srand().
http://www.cplusplus.com/reference/cstdlib/srand/

EDIT: Well, I say must, but it doesn't have to be.

It's just that rand() produces a random-looking sequence of numbers based on a special number called a seed. Without giving a random seed the sequence is always the same...

A good seed is what's returned by time(), as the linked article suggests.
Last edited on
Well if you look at the code, i did seed it with time, but its still not working correctly
What does 'sometimes rand() does not work or just sits there' mean?

Are you upset with what rand() returns? It's actually quite random... if you're unhappy with a particular sequence it spits out at any time there's not much anyone can do to help you, unless you can describe exactly what kind of sequence you want.
Well, i want it to give me a number between 0 and 9, including 0 and 9. it works sometimes and updates the game board, but sometimes it doesnt do anything and just lets the user use his turn again.
My problem is that sometimes the rand() function does not work

its not rand() so hense it must be your code.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int main(){
    srand(time(0));
    for (int i=0; i<10; i++)
        cout << rand() % 10 << endl;
}

1
2
3
4
5
6
7
8
9
10
5
1
0
1
9
6
5
3
8
0
Well i understand that, but i dont know whats wrong with my code, or why it works sometimes and not other times.
Then it sounds like your issue lies with the input stream.

The input stream will only pause execution and ask for input when there's nothing in the current buffer, if there are still other characters left over from when you last extracted input it will just use that instead.

On your OS and with your compiler it's quite possible using cin.sync() can be used to clear the buffer and give you the kind of style you want... try adding cin.sync() before you read input every time?

http://www.cplusplus.com/reference/istream/istream/sync/
Topic archived. No new replies allowed.