Rand not working correctly

For some reason i cant get it to spit out random numbers, both numbers it generates are the same.
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
 #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int	num1,  //holds random numbers
		num2;

	int answer; //holds the solution

	char	ch;  //holds a character for finishing the program

	//min and max random number values
	const int	maxValue = 300;
	const int	minValue = 1;

	//get the system time
	unsigned seed = time(0);

	//seed the random number generator
	srand(seed);

	//finds num1 and assigns the value to num1
	num1 = rand() % maxValue + minValue;

	//finds num2 and assigns the value to num2
	num2 = rand() % maxValue + minValue;

	//displays the problem for the user to solve
	cout << right << setw(4) << num1 << endl;
	cout << "+" << num1 << endl;

	//holds the program until user presses a key
	cout << "Press the ENTER key when finished solving" << endl;
	cin.get(ch);

	//calculates the answer to num1 + num2
	answer = num1 + num2;

	//displays the answer
	cout << "The answer is " << answer << "." << endl;

	return 0;
}


however, i did some tests on a separate program and it worked fine.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int num1, num2;
	unsigned seed = time(0);

	srand(seed);

	cout << rand() << endl;
	cout << rand() << endl;
	cout << rand() << endl;


	num1 = rand() % 300 + 1;
	cout << num1 << endl;

	num2 = rand() % 300 + 1;
	cout << num2 << endl;


	return 0;
}


What am i missing?
Thanks.
The problem is that you aren't outputting num2 in line 34. You are still outputting num1. You just have to change that. Both numbers are different. But yes. You are outputting num1 + num1.
Wow... I can't believe i missed that....

Thanks, it works. Haha
Last edited on
Topic archived. No new replies allowed.