Random Numbers

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
// Includes
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// Main Function
int main()
{
	srand(time_t(NULL));
	int random1 = 1 + (rand() % 1000);
	int random2 = 1 + (rand() % 1000);
	double answer;
	double correct;

	cout << "This program is a math tutor.\n\n";

	cout << "Generates 2 random numbers." << endl;
	cout << "\n\n" << "   " << random1 << endl;
	cout << " + " << random2 << endl;

	cout << "\n\nPlease enter your answer: ";
	cin >> answer;

	correct = random1 + random2;

	if (answer == correct)
	{
		cout << "Good Job, you got it right.";
	}
	else
	{
		cout << "OH NO, you got it wrong." << endl;
		cout << "The correct answer is: " << correct << endl;
	}
	return 0;
}


Everything is right, except the random numbers keep coming up as the same number, i cant get them to actually be random.
Yes, and thats because of this statement: srand(time_t(NULL));
Making it: srand(time(NULL)); must randomize it for you.

More info: http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
and: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Hope it HELPS!!!
Last edited on
Instead of

srand(time_t(NULL));

use

srand( time( NULL ) );
But if i change it to that this warning shows up.

1>c:\users\grimm\documents\visual studio 2012\projects\mathtutor\mathtutor\mathtutor.cpp(11): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
Last edited on
Nevermind, fixed the warning, all i had to do was make it srand((unsigned int) time(NULL));. Thanks for you help guys.
Topic archived. No new replies allowed.