   rand() specifiying lowest range....
  Been teaching myself C++ for last month, finally got "Beginning C++ through game programming 2nd edition" yesterday.Chapter 2 excrercise is number guessing game, guess computers number. this worked fine.  Then there is another excersize where the roles are reversed.  The computer now guesses the number.  There is no code for this excersize, so I do not know if this is a correct code, it works but displays almost all "too high!" in the guess. Is this a result of the "para"-randomness of rand or an incorrect use of rand() structure? using Dev-C++#include <cstdlib>#include <iostream>#include <ctime>using namespace std;int main(){    srand(time(0));    int usern;    int min=1;    int max=100;    int tries=0, guess;   cout<<"i'll guess your number\n";    cout<<"enter a number:";    cin>>usern;   do {guess=rand()%max+min;++tries;if(guess>usern){max=guess;cout<<"Too high!\n";}if(guess<usern){min=guess;cout<<"Too low!\n";} }while(guess !=usern); cout<<"was it "<<usern<<"?"; cout<<"\n\n\n\n\n";    system("PAUSE");    return EXIT_SUCCESS;}
  There are two things,First, the line: rand()%max+min;is a bit misleading, the actual range of the output is [min, min+max-1], this will affect the computers guessing.Secondly it depends on what the user inputs, if I were to give it initially the value 4, then the majority of responses would be higher.   ;  This is better:range = max - min;randomNumber = rand() % range + min;But it isn't correct... imagine if you were using a random range of 1 to 5 to pick a number from 1 to 3!  That would give you possibilities [1, 2, 3, 1, 2], meaning that 3 was only half as probable as the other 2, which is wrong.Try something like...randomNumber = round(((double)rand() / (double)RAND_MAX) * range) + min;   ;  Hello,I am actually using that book to refresh my C++ skills, and I am on Chapter 6 right now.  Well it does what the book expect you to do, and I will expect first time learners will get around the same thing. Also, I noticed you went beyond and made the computer AI smarter by having it set the range based on the guess.  Just listen to the people who already told you about how to fix the guess, but other than that I have to say it was good.Also, some tips that I got told I will let you know.-Avoid using namespace std.  It declares a lot of extra variables you don't need.  I know in the book it talked about that in Chapter 1.  Instead use the following:using std::cout;using std::cin;You will place this before the main function.-Avoid using system("PAUSE") because it does not work for everyone because of portibility.  Instead use the following:cin.ignore() //this ignores the previoud cin.  You will probably need it since you already had something cin.cin.get() //this waits for a key press by the user.I am glad you found out how to do it one your own.~Carl J. Loucius   ;range = max - min;randomNumber = rand() % range + minI used this & i confuse myself less. While i see that it not a good statistical spread, it does what i need for lesson purpose. I tackle the depths of rand() another day. system("PAUSE"); return EXIT_SUCCESS;This just for dev, im assuming. While no problem using it while learning, yeah, bad habits!!started using the cin.ignore();cin.get();routine, feels much nicer!thx all.   ;  Here's some random number functions that might be helpful. They use an optimization taken from Data Structures for Game Programmers so they are faster than using the typical mod method (at least on windows) and have good distribution.//-------------------------------------------------------------// GetRandomFloat - returns a random float between min and max// - min, max must be > 0//-------------------------------------------------------------inline float GetRandomFloat(float min, float max){float range = max - min;return  ( (range*rand()) / (RAND_MAX+1)) + min; }//-------------------------------------------------------------// GetRandomInt - returns a random int between min and max//    - min, max must be > 0//-------------------------------------------------------------inline int GetRandomInt(int min, int max){int range = max - min + 1;return  ( (range*rand()) / (RAND_MAX+1)) + min; }