   guessing game probs
  I have made a guess the number game but the random number is always 42.why?Here is the code.#include <iostream>#include <ctime>using namespace std;int main(void){int guess, num;    bool guessed = false;num = (rand() % 100) + 1;srand(time(NULL));while (guessed==false){cout << "Try to guess the secret number between 1 and 100: " << endl;cin >> guess;if ( guess == num ){cout << "You have guessed the number!!!!" << endl;break;}else if ( guess < num ){cout << "Your guess was too low." << endl;cout << "Guess again." << endl;}else{cout << "You guessed too high." << endl;cout << "Guess again." << endl;}}return 0;}   
  bxela1,Short Answer:You're calling rand() before calling srand().  Long Answer:Although rand() generates a series of random numbers based on a seed, the sequence is always the same.  So if you were to seed it with the same number and generate a string of 10 numbers, twice in a row, you'd have the same sequence of 10 numbers.  In this case you're not seeding it before generating a number, so it assumes a default seed.  Hence, your sequence is always the same.  By seeding it with the time, you'd be guaranteeing a different sequence each time you ran the program.Cheers!;  This is because the rand() function that comes with the C++ standard libraries is crap.If you want to get a different number each time you play the game all you'd need to do is put:srand([ctimes way of getting time here preferably in millisecs])But you'll probably find that if you did this:for(int i=0; i<100; i++){  srand(GetTimeInMillisecs());  std::cout << (rand() % 100) + 1 << "\n";}You'll get a list of 100 identacal numbers.There are better number generaters out there, although I can't remember any ATM.   ;  Well, srand only needs to be called once. However, it is worth keeping in mind that if you need repeatable random numbers, or multiple generators, or different distributions, you should look into the Boost Random Number Library. It has everything you need to generate high quality pseudo-random numbers.   ;Quote:Original post by Peter ConnThis is because the rand() function that comes with the C++ standard libraries is crap.If you want to get a different number each time you play the game all you'd need to do is put:srand([ctimes way of getting time here preferably in millisecs])But you'll probably find that if you did this:for(int i=0; i<100; i++){  srand(GetTimeInMillisecs());  std::cout << (rand() % 100) + 1 << "\n";}You'll get a list of 100 identacal numbers.There are better number generaters out there, although I can't remember any ATM.You only seed your random number generator once, at the beginning of the program.  If you keep re-seeding within a super fast loop, and then printing it out, no milliseconds have elapsed, and thus, each time you seed the generator you're seeding it with the same value.  Pull the srand() out of the for-loop and it will work as expected.  For the record, rand() is not crap...it works fine for most common purposes.  Its only "crap" when you've got an application that has a mathematical need for a more even spread of the results...ie. guarenteeing all numbers within range have been hit before duplicating values...but then, is that truly random, or just diverse?Cheers!   ;  Ah, thanks for that.Btw what is ctime's meathod for getting the current millisecs, I always used GetTickCounts from <windows.h>?   ;  std::time() will return seconds elapsed since 00:00 hours, Jan 1, 1970 UTC from the system clock as a long. Alternatively std::clock() returns milliseconds since process start. The first is probably a better seed for srand.   ;Quote:I have made a guess the number game but the random number is always 42.42? Thats not a bug, its an omen.   