How to get computer to guess a number given only "guess higher" or "guess lower" commands?

1
2
srand((unsigned)time(0));
	int MyNumber = rand()%(100);


I have this (I don't understand what it means) but I am able to generate a random number. I want the console to display a random number, then the user to tell the console either higher or lower, and in turn the console with generate another number that is (hopefully) closer to the users number.
srand((unsigned)time(0));
This will initialise the random number generator with a seed based on the current time. This ensures every time you run your application the list of numbers generated from the random number generator will be different.

int MyNumber = rand()%(100);
This says generate a random number rand() and then using the modulus operation take the remainder of that number when divided by 100. Store the result in MyNumber.

Functionally, this will return a random number between 0 and 99.

If you want it to generate a random number between 2 values (high and low) you need to calculate the difference between them. Once you have the difference between them you can take a random number and get the remainder when divided by the difference. Finally add this new random number to your low value and you have a random value between high and low.

note: When calculating difference you need to consider if low and high are both value values.

e.g high = 9, low = 6.
difference = 9 - 6 = 3.

Difference is wrong if both low and high cannot be guessed. So you need to adjust it based on if your low/high are valid.


Yes. It would've been quicker to type you the 2 lines of code required, but then you won't learn anything :)
Thanks for the clarification!
Topic archived. No new replies allowed.