A Problem with this simple game.

Well, Im very new to C++. Ive made very simple projects lately and I thought i could give a game a try. As my first game I wanted to go a simple as possible. A guess the number game. The problem is with the random number generating. I'm not good at generating random numbers at all... The part always generates 1 when it should be picking 1 between 1 and 5. How can I fix this. Here is the entire code.

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

using namespace std;

int main()
{
   int number;
   int guess;

   number = rand() % 5;

   cout << "A Random number between 1 and 5 has been generated. Try and guess it." << endl;
   cin >> guess;

   if (guess == number)
   {
              cout << "Good job! You correctly Guessed the Number!" << endl;
    }
    else if (guess != number)
    {
              cout << "That number is incorrect!" << endl;

    }

    }
try putting #include <time.h>
after #include <iostream>
and
try adding
srand(time(NULL));
before number = rand() % 5;
Thanks. Im very new.. This fourm probably makes me look like a noob but... Thanks for the help.
rand()%5 can also be 0, just so you know.
+1 PiMaster.

rand() % 5 gives you a range of [0..5). That is, sometimes 0, and never 5.

if you want [1..5] then do this: (rand() % 5) + 1;
Thanks. I will change that.
Topic archived. No new replies allowed.