Possible Duplicate:
Generating random integer from a range
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:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(static_cast <unsigned int> (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 << "Please pick a number between 1 - 100. I will guess your number. Don't tell me what it is!\n\n"; do { cout << "Are you ready? (y/n)\n\n"; cin >> ready; if (ready == 'y') { do { cout << "Is your number " << compGuess << "?\n\n"; cout << "High, Low or Success?"; ++tries; cin >> 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 // <---- Not producing the desired result success = false; } else if (highLowSuccess == 's') //Executes code if the computer's guess was correct. { cout << "I guessed your number! It only took me " << tries << " tries!"; success = true; } } while (success != true); } else { continue; } } while (ready != 'y'); return 0; }
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.
Forgive me if the answer is really simple, I'm a noob programmer. xD
The Excel RANDBETWEEN function returns a random integer between given numbers. RANDBETWEEN is a volatile function recalculates when a worksheet is opened or changed. This formula is then copied down from B5 to B11. The result is random numbers between 1-100.
The Excel RANDBETWEEN function returns a random integer between two given numbers. RANDBETWEEN recalculates each time a worksheet is opened or changed.
Generate Random Numbers with the RAND function. The first way I will show you is the easiest way to generate random values in Excel. There is a very simple RAND function that requires no parameters and will generate a random number between 0 and 1.
To generate a random number between min and max, use:
int randNum = rand()%(max-min + 1) + min;
(Includes max and min)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With