Reverse "Guess the Number" program I recently got to a certain exercise in a book I am reading, and am looking for feedback. It is a program in which the user picks a number and the computer uses algorithms to guess it. If there is any way that I can improve my code, feedback is really appreciated!

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;
using namespace std;

int main()
{
    srand(static_cast&lt;unsigned int&gt;(time(0))); //seed random number generator
    int compNumber = rand() % 100 + 1;

    int yourNumber;
    cout &lt;&lt; "Please input a number between 1 and 100" &lt;&lt; endl;
    cin &gt;&gt; yourNumber;

    cout &lt;&lt; "Ok! I'm gonna try to guess it!" &lt;&lt; endl;

    while (compNumber != yourNumber)
    {
        if (compNumber &gt; yourNumber)
        {
            do
            {
                --compNumber;
            } while (compNumber != yourNumber);
        }

        else if (compNumber &gt; yourNumber)
        {
            do
            {
                ++compNumber;
            } while (compNumber != yourNumber);
        }

    }

    cout &lt;&lt; "I guessed it! Your number is " &lt;&lt; compNumber &lt;&lt; endl;

    return 0;

}
Sure this generates a random number from 1 -> 100.

int compNumber = rand() % 100 + 1;


BUT not all the numbers have an even probability. Assuming RAND_MAX is 32768 (a common value). Then the number 1->68 have a slightly higher probability than the number 69->100.

To get an even distribution you need to compensate for this:

int compNumber
do
{
    compNumber = rand();
}
while (compNumber &gt; (RAND_MAX / 100 * 100));
compNumber = compNumber % 100 + 1;


Better yet learn to use the modern random number generator that is built into C++.

// I have not tested this.
// Just copied and pasted from: http://stackoverflow.com/a/19666713/14065
// This is the modern equivalent of srand()
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution&lt;int&gt; dist(1, 100);


// Then usage is.
// equivalent of rand().
std::cout &lt;&lt; dist(mt) &lt;&lt; "\n";


This does not look correct:

    if      (compNumber &gt; yourNumber)
    {}
    else if (compNumber &gt; yourNumber)
    {}


The conditions look the same to me.

This does not look like guessing.

        do
        {
            --compNumber;
        } while (compNumber != yourNumber);


This looks like the computer counting down until it reaches your number.