Problems with rand() and srand()

Hello there. I'm in the process of writing a tick tac toe program. The game isn't finished, but it's completed enough that I've found a substantial error with the computer opponent. The program isn't correctly generating new random numbers every time the function is called. I would be forever grateful if somebody could look through and tell me what the problem is. Thank you very much.

Here is the function with the random number generator:
1
2
3
4
5
6
7
8
9
10
11
12
13
void Tic_Tac::cpu()
{	
	int one = 0;
	int two = 0;	
	
	srand(one);
	one = rand() % 3;
	
	srand(two);
	two = rand() % 3;
		
	board[one - 1][two - 1] = "|_O_|";
}


Here is the entire program:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <cstdlib>
using namespace std;

const int LIMIT = 3;

class Tic_Tac{
	public:
		void setup();
		void print();
		bool win();
		void input();
		void move();
		void cpu();
	
	private:
		string board[LIMIT][LIMIT];
		char x;
		int y;
		bool combo1();
		bool combo2();
		bool combo3();
		bool combo4();
		bool combo5();
		bool combo6();
		bool combo7();
		bool combo8();
		
		
};

int main()
{
	Tic_Tac game;
	
	cout << "WELCOME to Tic Tac Toe!" << endl << endl;
	
	game.setup();
	
	do{	
		game.print();
		game.input();
		game.move();
		game.cpu();
	}while(!game.win());
	
	game.print();
	cout << "CONGRATULATIONS! You won!" << endl;
	
	return 0;
}

void Tic_Tac::setup()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			board[i][j] = "|___|";
		}
	}
}

void Tic_Tac::print()
{
	char a = 'a';

	cout << " __1____2____3__" << endl;
	
	for(int i = 0; i < LIMIT; i++){
		cout << a;
		a++;
		for(int j = 0; j < LIMIT; j++){
			cout << board[i][j];
		}
		cout << endl;
	}
	
	cout << endl;
}

bool Tic_Tac::win()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if(combo1())
				return true;
			if(combo2())
				return true;
			if(combo3())
				return true;
			if(combo4())
				return true;
			if(combo5())
				return true;
			if(combo6())
				return true;
			if(combo7())
				return true;
			if(combo8())
				return true;
		}
	}
	
	return false;
}

void Tic_Tac::input()
{
	cout << "Input your coordinates: " << endl;
	cin >> x;
	cin >> y;
	cout << endl;
}

void Tic_Tac::move()
{
	int a;
	
	if(x == 'a'){
		a = 1;
	}
	if(x == 'b'){
		a = 2;
	}
	if(x == 'c'){
		a = 3;
	}
	
	board[a - 1][y - 1] = "|_X_|";
}

void Tic_Tac::cpu()
{	
	int one = 0;
	int two = 0;	
	
	srand(one);
	one = rand() % 3;
	
	srand(two);
	two = rand() % 3;
		
	board[one - 1][two - 1] = "|_O_|";
}

bool Tic_Tac::combo1()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][0] == "|_X_|") && (board[0][1] == "|_X_|") && (board[0][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo2()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[1][0] == "|_X_|") && (board[1][1] == "|_X_|") && (board[1][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo3()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[2][0] == "|_X_|") && (board[2][1] == "|_X_|") && (board[2][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo4()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][0] == "|_X_|") && (board[1][0] == "|_X_|") && (board[2][0] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo5()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][1] == "|_X_|") && (board[1][1] == "|_X_|") && (board[2][1] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo6()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][2] == "|_X_|") && (board[1][2] == "|_X_|") && (board[2][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo7()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][0] == "|_X_|") && (board[1][1] == "|_X_|") && (board[2][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo8()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[2][0] == "|_X_|") && (board[1][1] == "|_X_|") && (board[0][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}
You should call srand() once at the beginning of your program, not every time you want a random number. You'll probably also want to use an (effectively) arbitrary seed, too, like time(0).

You're seeding the generator with 0 every time, so it will always generate the same random number for 'one'. Then you use that always-the-same number for the next seed, ensuring that 'two' will also always be the same number.
I'm fairly unfamiliar with rand() and srand(), can you explain a little more what you mean by 'arbitrary seed'?

I made the following changes to my code; note the 'one' and 'two' declarations in main along with srand() and them being passed into cpu(). However, the program still only generates one position once and after 2 turn I receive a segmentation fault. Think you can tackle it again?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <cstdlib>
using namespace std;

const int LIMIT = 3;

class Tic_Tac{
	public:
		void setup();
		void print();
		bool win();
		void input();
		void move();
		void cpu(int one, int two);
	
	private:
		string board[LIMIT][LIMIT];
		char x;
		int y;
		bool combo1();
		bool combo2();
		bool combo3();
		bool combo4();
		bool combo5();
		bool combo6();
		bool combo7();
		bool combo8();
};

int main()
{
	Tic_Tac game;
	int one = 0;
	int two = 0;
	
	cout << "WELCOME to Tic Tac Toe!" << endl << endl;
	
	game.setup();
	
	srand(one);
	srand(two);
	
	do{	
		game.print();
		game.input();
		game.move();
		game.cpu(one, two);
	}while(!game.win());
	
	game.print();
	cout << "CONGRATULATIONS! You won!" << endl;
	
	return 0;
}

void Tic_Tac::setup()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			board[i][j] = "|___|";
		}
	}
}

void Tic_Tac::print()
{
	char a = 'a';

	cout << " __1____2____3__" << endl;
	
	for(int i = 0; i < LIMIT; i++){
		cout << a;
		a++;
		for(int j = 0; j < LIMIT; j++){
			cout << board[i][j];
		}
		cout << endl;
	}
	
	cout << endl;
}

bool Tic_Tac::win()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if(combo1())
				return true;
			if(combo2())
				return true;
			if(combo3())
				return true;
			if(combo4())
				return true;
			if(combo5())
				return true;
			if(combo6())
				return true;
			if(combo7())
				return true;
			if(combo8())
				return true;
		}
	}
	
	return false;
}

void Tic_Tac::input()
{
	cout << "Input your coordinates: " << endl;
	cin >> x;
	cin >> y;
	cout << endl;
}

void Tic_Tac::move()
{
	int a;
	
	if(x == 'a'){
		a = 1;
	}
	if(x == 'b'){
		a = 2;
	}
	if(x == 'c'){
		a = 3;
	}
	
	board[a - 1][y - 1] = "|_X_|";
}

void Tic_Tac::cpu(int one, int two)
{		
	one = rand() % 3;
	
	two = rand() % 3;
		
	board[one - 1][two - 1] = "|_O_|";
}

bool Tic_Tac::combo1()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][0] == "|_X_|") && (board[0][1] == "|_X_|") && (board[0][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo2()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[1][0] == "|_X_|") && (board[1][1] == "|_X_|") && (board[1][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo3()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[2][0] == "|_X_|") && (board[2][1] == "|_X_|") && (board[2][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo4()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][0] == "|_X_|") && (board[1][0] == "|_X_|") && (board[2][0] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo5()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][1] == "|_X_|") && (board[1][1] == "|_X_|") && (board[2][1] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo6()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][2] == "|_X_|") && (board[1][2] == "|_X_|") && (board[2][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo7()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[0][0] == "|_X_|") && (board[1][1] == "|_X_|") && (board[2][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

bool Tic_Tac::combo8()
{
	for(int i = 0; i < LIMIT; i++){
		for(int j = 0; j < LIMIT; j++){
			if((board[2][0] == "|_X_|") && (board[1][1] == "|_X_|") && (board[0][2] == "|_X_|")){
				return true;
			}
		}
	}
	
	return false;
}

The C++ rand() function uses a reasonably simple algorithm to return a random number between 0 and RAND_MAX, which differs between platforms.

For the algorithm to work, you need to seed it with a number. Call it a basis from which the algorithm can do its work.

If you seed it with a fixed number, you'll get the same results each time the program runs. The math behind the algorithm is fixed, so if your program gives it the same seed every time, you're going to get the same results every time, which is what is currently happening in your code.

A common way to get a pseudo-random value is to seed the generator using the system clock, as Zhuge pointed out.

 
srand( time( NULL ) );


The value will be differ between runs of the program as the system time will be different.

You then call the rand() function to get your random number and limit the range using the modulus operator. The maths to this is quite obvious, but give me a shout if you need me to explain further.
Last edited on
Got it. Thanks all!
Topic archived. No new replies allowed.