# Thread: couple questions for very small random number program

1. ## couple questions for very small random number program

Hey I am trying to make a smal random number game just for practice and have 2 questions. First I'm wondering if im generating the random number (between 1 and 100) the correct way. Second I am wondering how to loop this program (preferably with a for loop) so it will ask more than 1 time and then produce the answer, if the answer isnt correct then I obviously want it to ask then answer again. I am not sure you can use a for with the if-else? Anyways any help will be appreciated, thanks.

Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main ()
{
int guess;
double random;
int i;

cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
cin >> guess;

srand(time(NULL));
random = rand() % (100 + 1);

if (random > guess)
cout << "Guess was too big\n";
else if (random < guess)
cout << "Guess was too small\n";
else if (random == guess)
cout << "You got it!\n";

system ("pause");

return 0;

}```

2. Remove the brackets around 100 + 1. The +1 is supposed to occur after the %100.
Also, while what you have is fine for one call to rand(), make sure that when you generate more calls to rand that you don't make more calls to srand. Call srand only once in your entire program, preferably near the top of main.

3. Thanks. was just missing some brackets..but one more thing I can't figure out is why no matter what my program always prints "Yout number is too big". Did I write my else-if wrong or something? Thanks

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main ()
{
int guess;
double random;
int i;
srand(time(NULL));

for (i = 1; i <=2; i++)
{
cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
cin >> guess;

random = rand() %100 + 1;

if (random > guess)
cout << "Guess was too big\n";
else if (random < guess)
cout << "Guess was too small\n";
else if (random == guess)
cout << "You got it!\n";
}

system ("pause");

return 0;

}

4. Code:
```
if (random > guess)
cout << "Guess was too big\n";
else if (random < guess)
cout << "Guess was too small\n";
else if (random == guess)
cout << "You got it!\n";```
You need to put 'guess' first.
As in:
Code:
```   if (guess > random)
cout << "Guess was too big\n";
else if (guess < random)
cout << "Guess was too small\n";
else if (guess == random)
cout << "You got it!\n";```
And your random number is going to be between 1 and 101, not 1 and 100.
A formula for random numbers is: rand() % (max - min) + min
So that would be: rand() % (100 - 1) + 1
which could be simplified to rand() % 99 + 1

You could use a while loop to make the program continue to ask for your guess.

Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main ()
{
int guess;
double random;
int i;
srand(time(0)); //Don't use NULL?

cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
random = rand() % 99 + 1;

while (true) //Loop infinitely (or until a 'break' command is called)
{
cin >> guess; //Ask for input

if (guess > random)
cout << "Guess was too big\n";
else if (guess < random)
cout << "Guess was too small\n";
else
{
cout << "You got it!\n";
break; //Break out of the while loop!
}
} //End bracket for while loop

system("PAUSE");

return 0;

}```

5. but one more thing I can't figure out is why no matter what my program always prints "Yout number is too big". Did I write my else-if wrong or something? Thanks
This is because you keep resetting the value of the 'random' variable with a random number after the user guesses.
I fixed this in the code I gave you. But if you want to still use your code, then put the
random = rand() %100 + 1;
before the user guesses AND outside of the for loop (so that it is initialized with a random value only once).

6. Thanks guys got it figured out.

7. Originally Posted by Flaug
You And your random number is going to be between 1 and 101, not 1 and 100.
A formula for random numbers is: rand() % (max - min) + min
So that would be: rand() % (100 - 1) + 1
which could be simplified to rand() % 99 + 1
Incorrect!
The formula is rand() % (max - min + 1) + min

rand() % 100 gives you a number from 0 to 99, and adding one gives you 1 to 100.

Also, when you have to use comments like
Code:
`//End bracket for while loop`
You've either failed at doing correct indentation, or at keeping your function to an appropriate length, or both.

8. You've either failed at doing correct indentation, or at keeping your function to an appropriate length, or both.
Or I'm just too lazy to press the spacebar twenty times to indent the code while in the reply text box.

Incorrect!
The formula is rand() % (max - min + 1) + min
Whoops! Guess I screwed that one up.....