-1

I'm trying to make a guessing game program where the user thinks of a number between 1 and 100, and then the computer randomly generates(guesses) a number between 1 and 100. Then the user tells the computer if their number is higher(H), lower(L), or the same(Y) as the guessed number. The problem I am having is changing the range that the computer generates its guess in.

So let's say my number is 43 and the computer generates 15 as its guess. I would input H, indicating that my number is higher than its guess. The computer then changes the parameters to generate a number between 16 and 100.

int high = 100, low = 1;
char answer;

Setting my variables.

srand(time(0));

I have this to truly randomize the generation.

int guess = rand() % high + low;
cout << "Is your number " << guess << "?\n";
cin >> answer;

Generate the number, ask if the number is what I'm thinking of, and then ask me to answer if it is higher, lower, or correct.

Then the segment that checks the answer and determines how to change the range of the randomly generated number.

switch (answer)
    {
    case 'H':
        low = (guess + 1);
        high = (high - guess);
        break;
    case 'L':
        high = guess;
        break;
    default:
        break;
    }

I guess what I'm not understanding is how rand() truly works. Like I understand the whole

rand() % 100 //range of 0 and 99
rand() % 100 + 1 // range of 1 and 100

But I want to know how I can change the parameters so that I get

rand() % max + min // range of min and max

while I constantly change and update the values for max and min.

  • 3
    Do you mean if `min = 3` and `max = 10` then you want a guess somewhere between 3 and 10? If so, then you would just do `rand() % (max + 1 - min) + min`. – Cornstalks Jan 23 '16 at 21:08
  • If you can, use a better RNG such as [the ones built into C++11](http://en.cppreference.com/w/cpp/numeric/random) or [Boost](http://www.boost.org/doc/libs/1_60_0/doc/html/boost_random.html). – krlmlr Jan 23 '16 at 21:18

1 Answers1

4

You're basically on the right track. Using rand() % n will give you a value in the half-open range [0, n). So if you want numbers from 0 to 100, inclusive, use rand() % 101. When you have a maximum and a minimum value, n is max - min + 1. That gives you a range of values starting at 0. To get a range of values that starts at min, just add min to the value.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • The random numbers will not be distributed uniformly if the range of `rand()` is not a multiple of `n`. – krlmlr Jan 23 '16 at 21:19
  • @krlmlr - yes, that's what you said in your comment on the question. – Pete Becker Jan 23 '16 at 21:42
  • You could do better with `rand()`, too. See http://c-faq.com/lib/randrange.html plus a few duplicate questions on this site. – krlmlr Jan 23 '16 at 21:47
  • 1
    @krlmlr - none of that is better. The only way to get rid of the non-uniformity is to discard some values. Rescaling just changes which values you get more frequently. But please, that's far enough down this rathole. If you have more to way, write your own answer. – Pete Becker Jan 23 '16 at 23:15
  • Rejection sampling is mentioned in the FAQ I linked. ("When `N` is close to `RAND_MAX`...".) – krlmlr Jan 24 '16 at 16:11