Generating random numbers

I'm currently working on a program where the user and computer take turns guessing a number. I'm completely stumped on how to generate a random number lower or higher than the last.

For example:

I choose the number 50 and the computer must guess exactly 50 to win (or end the game.) So if the computer guesses 60, a message that says "too high" is displayed. On the next turn the computer must guess a random number between 60 in order to add intelligence to the program.

Please let me know if any additional information is needed to help me.
I would probably create two variable, lower and upper, and just guess a number between them. Then use the answer to set the corresponding bound.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdlib>  // for rand
#include <ctime>  // for time
...
// seed the RNG -- do this *only once* at program startup
srand((unsigned)time(0));

// 
// to get a random number between [0 - 99]
int num = rand() % 100;

// to get a random number between [L - H)
int range = H-L;
int num = (rand() % range) + L;


If you want the computer to take "smart" guesses, keep a range of where the number can be (like 'L' and 'H' in the above example), and if it guesses too low, increase the low end of the range, and if it guesses too high, decrease the high end of the range.
Topic archived. No new replies allowed.