title: How do I generate a random number between two variables that I have stored?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range">Generating random integer from a range</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying to create a program where the computer guesses a number the user has in his/her mind. The only user input required is whether the guess was too high, too low, or correct. I'm having a problem generating a random number between two variables that store the min and max based on previous guesses. Here is my code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
      #include &lt;cstdlib&gt;
      #include &lt;ctime&gt;
  
      using namespace std;
  
      int main()
      {
          srand(static_cast &lt;unsigned int&gt; (time(0)));
  
          int compGuess = rand() % 100 +1; //Generates number between 1 - 100
          int highestNumber = 100;
          int lowestNumber = 1;
          char ready;
          char highLowSuccess;
          bool success;
          int tries = 0;
  
  
          cout &lt;&lt; "Please pick a number between 1 - 100. I will guess your number. Don't tell me what it is!\n\n";
  
  
          do
          {
              cout &lt;&lt; "Are you ready? (y/n)\n\n";
              cin &gt;&gt; ready;
  
              if (ready == 'y')
              {
                  do
                  {
                      cout &lt;&lt; "Is your number " &lt;&lt; compGuess &lt;&lt; "?\n\n";
                      cout &lt;&lt; "High, Low or Success?";
                      ++tries;
                      cin &gt;&gt; highLowSuccess; //User input telling the computer whether its too high, too low, or a success
  
                      if (highLowSuccess == 'h') //Executes code if number guessed was too high.
                      {
  
                          highestNumber = compGuess - 1; //Stores variable indicating the highest possible number based on user input
                          compGuess = rand() % highestNumber +1; //Generates a new random number between 1 and the new highest possible number
                          success = false;
                      }
  
                      else if (highLowSuccess == 'l') //Executes code if number guessed was too low.
                      {
                          lowestNumber = compGuess + 1;//Stores variable indicating the lowest possible number based on user input
                          compGuess = (rand() % highestNumber - lowestNumber + 1) + lowestNumber // &lt;---- Not producing the desired result
                          success = false;
                      }
  
                      else if (highLowSuccess == 's') //Executes code if the computer's guess was correct.
                      {
                          cout &lt;&lt; "I guessed your number! It only took me " &lt;&lt; tries &lt;&lt; " tries!";
                          success = true;
                      }
  
  
                  } while (success != true);
              }
  
  
              else
              {
               continue;
              }
  
         } while (ready != 'y');
  
  
  
      return 0;
  
      }
  </code></pre>
  
  <p>highestNumber is what the max should be and lowestNumber is what the min should be. I need an equation that lets me generate a random number while taking the highest and lowest possible numbers into account.</p>
  
  <p>Forgive me if the answer is really simple, I'm a noob programmer. xD</p>