C++ Random Question
-
:)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!
-
:)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!
Put the line random = rand(); in the do while block and better add this to the start of the main srand(time(NULL)); this way the seed is randomly generated too (you wont start with the same population over and over again) Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
:)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!
You need to call
srand()
first, otherwiserand()
will always return the same number. A line likesrand((unsigned)time(NULL));
before the call torand()
should do the trick. -
:)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!
To elaborate a bit more on the previous response, it is recommend that you first seed the random number generator. If you wish a consistent pattern of values then always seed with the same value. If not then you can use this :
srand( time(NULL) % RAND_MAX );
to obtain values between 0 and 100 then you can do this :random = rand() % 101;
The modulus operator returns the remainder of the division operation of the two numbers. This has the effect of clamping the return value between 0 and one less than the dividend. __________________________________________ a two cent stamp short of going postal. -
:)First off apols if this is in the wrong section i am new here I have this code now and try as i may i cannot get the random number to change it is always 41 when it should be between 0-100. I am not that good at C++ and would appreciate any help can i please reinforce the basis that my knowledge is very basic thanks for any help guys. #include #include int main(void) { // declare our variables // Guess entered by user and random number int guess, random; random = rand(); // Creates a loop for the main program do { cout << "My High-Low game!\n\n"; cout << "Please enter a number between 0 and 100: "; cin >> guess; cout << "You entered " << guess << "!\n"; if (guess == random) { cout << "Correct guess, the answer is " << guess << "!\n"; } else { if (guess > random) { cout << "Your guess is too high!\n"; } else { cout << "Your guess is too low!\n"; } } // Terminating condition for the loop // Program stops when the guess equals the random number } while(guess!=random); // This command pauses the program system("PAUSE"); return 0; } This Better ******* Work!
rand()
returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers. So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this:srand(static_cast<unsigned>(time(0)));
Make this function call before you use rand() for the first time. If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand().random = 1 + (rand() % 100);
rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99. Hope this helps, Cohen -
rand()
returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers. So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this:srand(static_cast<unsigned>(time(0)));
Make this function call before you use rand() for the first time. If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand().random = 1 + (rand() % 100);
rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99. Hope this helps, CohenThank you for the tips guys i will try them out i just have to figure a way fo getting it to give the user 8 trys max. Thanks for not talking to clever for me lol :) This Better ******* Work!
-
rand()
returns pseudo random numbers rather than real random numbers. This is why you always get the same value. You can work your way around this by setting the random seed, before you invoke rand(). Seed is a number which is used inside the rand() function to produce the sequence of these pseudo random numbers. So, different seed value gives you different sequence of numbers. Common trick is to give the current time as seed number as this gives you a different sequence each time you run your application. You can set the seed like this:srand(static_cast<unsigned>(time(0)));
Make this function call before you use rand() for the first time. If you wan't the numbers be in a spesified range (between 1 and 100) you must use the following line instead of just invoking rand().random = 1 + (rand() % 100);
rand() returns numbers between 0 (?) and RAND_MAX (== big number, depends on you compiler I think) and the above line takes a modulus from the returned value and this conveniently scales the result between 0 and 99. Hope this helps, Cohen -
Thank you for the tips guys i will try them out i just have to figure a way fo getting it to give the user 8 trys max. Thanks for not talking to clever for me lol :) This Better ******* Work!
bobski2200 wrote: i just have to figure a way fo getting it to give the user 8 trys max. Change
while(guess!=random);
to
while(guess!=random && num_tries < 8);
Start
num_tries
at 0 and increment it after each guess.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen