Help with random command.

Hello everyone, im doing this tic tac toe program for my class... it is done, but the last requisite is that the first move must be define randomly, and i dunno how to do it... i know i need rand command, but i dont have an idea how to implement in the code..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(y=0; y<3; y++){
        a[y]=0;
    }

    cout <<"Player 1: ";
    cin >>jug1;
    cout <<"Player 2: ";
    cin >>jug2;
    cout <<endl<<endl<<jug1<<". X"<<endl;
    cout <<jug2<<". O"<<endl<<endl;
    cout <<"| 1 | 2 | 3 |" << endl;
    cout <<"-------------" << endl;
    cout <<"| 4 | 5 | 6 |" << endl;
    cout <<"-------------" << endl;
    cout <<"| 7 | 8 | 9 |" << endl<< endl ;
    cout <<"Choose a number."<<endl<<endl;
    for(x=0; x<3; x++){
        for(y=0; y<3; y++){
            matriz[x][y]=' ';
        }
    


thanks, any help will be so much appreciated
http://www.cplusplus.com/reference/cstdlib/rand/

One of those 3 examples looks like what you need. I assume you need an integer between 0 and 8?
well, noy really... i just need the random number to define which player will make the first move... =S
All the information you need is in the link mutexe provided.
You just have to think about how to use it.

1
2
3
4
5
 
  int first_player; 

  srand(time(NULL);                    // Randomize the the seed 
  first_player = rand() % 2 + 1;  // Will result in 1 or 2 


Topic archived. No new replies allowed.