Random Numbers in C++

Hello all! Well, I'm attempting to generate a random number and make it equal to the variable "random_number". After the program makes a random number, the user will be prompted to guess what the number is, and based upon what they entered, it will tell them if they have guessed correctly, to high, or to low. Here's what I have so far:

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
#include <iostream>


using namespace std;

int user_guess, random_number;
int rand ( void );

int main()

{

random_number = rand();

cout << "A sale has occurred. Please enter payment." << endl;
cin >> user_guess;

if (user_guess == random_number)
    cout << "You have entered the exact amount. You're awesome!" << endl;

else if(user_guess < random_number)
    cout << "You have underpayed. Please enter again."  << endl;

else
    cout << "You have overpayed. Please enter again." << endl;


return 0;

}


Right now, I'm getting the error: "undefined reference to 'rand()'". There's more to this that I need to do (if over payed, need to spit back change, in coins / dollars, among others) but if you'd be so kind as to clarify how this random number generator works, that'd be great.
You need to #include <cstdlib>
Also read http://cplusplus.com/reference/clibrary/cstdlib/rand/ .
You will have to call srand once in your program, otherwise rand() will always generate the same sequence of numbers.
Topic archived. No new replies allowed.