rand() function only generating 42

I'm trying to create a simple game in C++ that generates a random number and, when the user guesses incorrectly, tells the user whether they need to guess higher or lower to guess the generated number. The problem is that the only number that's been generated is 42. I'm pretty sure that the rand() function has a predetermined set of random numbers to use, but I was wondering if there was a better way for me to do this without straying too far from the use of functions.
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
  //Aidan Satterwhite
//Assignment 10

#include <iostream>
#include <cstdlib> //I've also used <cmath> here, and the "random" number is still 42
using namespace std;

int main(){
	int random, guess;

	random = rand() % 100 + 1;

	cout << "Please guess a number: ";
	cin >> guess;

	while (random != guess){
		if (guess > random){
			cout << "Lower." << endl;
		}
		else if (guess < random){
			cout << "Higher." << endl;
		}
		cout << "Please guess a number: ";
		cin >> guess;
	}

	cout << "Congratulations! You guessed " << random << "!" << endl;

	return 0;
}
Add this
srand(time(NULL));

before

random = rand() % 100 + 1;
Last edited on
It says that "Identifier 'time' is undefined."
Last edited on
try #include <time.h>
Awesome, that worked! Thank you so much.
Topic archived. No new replies allowed.