### Random Number Generator?

How would I make a random number generator, that only generated integers from 1-50, and then can I change the highest number it guesses? Like later on be 1-15 or something? Thank you.
use rand() function.

Like:

x = rand() % 50 + 1

printf("%d", x);

this will print the random number generated by the rand function. I believe doing the 50 + 1 produces numbers 1-49 though? Idk, I forget. You should look up on how it works :P.

Also use srand(time(NULL)) at the beginning of your function otherwise the "random" numbers will be exactly the same every time you run the program.
It said time isn't declared in this scope?

 ``1234567891011121314151617`` ``````#include #include #include #include using namespace std; int main () { std::string NUMBER; cout <<" Want to play a game? Think of a number between 1-50, and type it. If you need a reminder of the number, type NUMBER. Caps or no caps. I'm going to guess numbers, type HIGHER or LOWER depending on my guess. Here we go."; getline (cin, NUMBER); srand(time(NULL)) x = rand()%50+1 printf("%d", x); return 0; }``````
Oh you need time.h library. sorry :P.

And make sure to declare int x; at the beginning of function.

And the way you have it set up, you cannot type higher or lower, you can only type a number then after you hit enter it will display the random number.

You will need a lot more. If you want it to keep guessing you will need some:
after first guess:
(use a while loop here to check if guessed number is equal to actual number).
printf("Press L for lower, or H for higher");
scanf("%c", variable);
if(variable == "L"){
x = rand() % x + 1;} //We now have it guessing between 1 and first number, thus a lower number.
else if(variable == 'H'){
x = rand() 50 + x;}
//add some printf()'s to print out guessed number.
else if( x == NUMBER){
printf("I guessed your number!") //some sort of confirmation message.

Also you are in c++ it seems while I only know C language :P So you might have to make the necessary conversions of C commands over to c++ commands, which shouldn't be that difficult.
Last edited on
The if (x == NUMBER) line doesn't work, the Code::Blocks didn't accept that?
I am not sure what Code::Blocks are since that seems c++... But make sure the number the user originally enters is put in the variable NUMBER (or whatever variable you choose). And the code I gave was like a quick sketch. If you attempted to copy what I had and run it I am sure it would not work, Those were things to implement, think about, and work with :P.
Here is the smallest code i could come up with, but no need for input on this one:

 ``123456789101112131415`` ``````#include #include // Needed for the true randomization #include using namespace std; int main () { int xRan; srand( time(0)); // This will ensure a really randomized number by help of time. xRan=rand()%15+1; // Randomizing the number between 1-15. cout << "Shows a random number between 1-15: " << xRan; return 0; }``````
Last edited on
This is what I have so far, thanks to LuckyShot primarily.

 ``12345678910111213141516171819202122232425262728293031323334`` `````` #include #include #include #include #include using namespace std; int main () { std::string NUMBER; std::string HIGHLOW; int xRan; cout <<" Want to play a game? Think of a number between 1-50, and type it. If you need a reminder of the number, type NUMBER. Caps or no caps. I'm going to guess numbers, type HIGHER or LOWER depending on my guess. Here we go."; getline (cin, NUMBER); srand( time(0)); xRan=rand()%50+1; cout << "I am guessing " << xRan; "Is your number higher or lower?"; getline (cin, HIGHLOW); if (HIGHLOW == ("higher")) { xRan=rand()%xRan+50; cout <<" I am guessing " <

However, the numbers don't work. Can someone help me making the LOWER or HIGHER numbers work? Based off of the guess? Like if the number you pick is 45, and it guesses 23, how do I limit it from 23-50, not 0-50?
Last edited on
I see now that i did it entirely opposite of you, i will try to rewrite it to work like yours.

The low/high number I fixed using this formula
 ``1234`` ``````userMaxRand = userMaxRand - userLowRand; // Subtracts what number you choose as low srand( time(0)); xRand=rand()%userMaxRand+userLowRand+1; // Randomizes between the numbers you choose. ``````

Here is the finalized template, it should work perfectly. If you want to remove the number of tries feature, just remove this from the code: `xTries > 1 &&` found in the while loop (the only one in this program). Good luck and hope this helped you on your game =)

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include #include using namespace std; int main () { int xRand, userMaxRand, userLowRand, xGuess, xTries=10; cout << "Please enter the two numbers you want to guess between." << endl << "Type the lowest number first and the high last, use a space between them: "; cin >> userLowRand; cin >> userMaxRand; cout << "Guess a number between " << userLowRand << " and " << userMaxRand << ": "; userMaxRand = userMaxRand - userLowRand; // Subtracts what number you choose as low srand( time(0)); xRand=rand()%userMaxRand+userLowRand+1; // Randomizes between the numbers you choose. while ( xTries > 1 && xRand != xGuess ) // Checks if you've guess right and count tries left. { cin >> xGuess; // Input on the guesses. if (xGuess > xRand) // Checks if the number you guess is too high. cout << xTries << " The number you guessed is too high, try again: "; else if (xGuess < xRand) // Checks if the number you guessed is too low. cout << xTries << " The number you guessed is too low, try again: "; xTries--; // This one subtracts number of tries you have left, same as xTries = xTries - 1 } cout << endl; // Just writes if you won or lost on a new line. if (xGuess == xRand) // Message printed if you won. cout << "You guessed right, the number was: " << xRand; else // Message printed if you lost. cout << "Sorry, you lost. The number was: " << xRand; return 0; }``````
Last edited on
Thank you so much!
Here is a prototype of what you are looking for. I must warn you, its not stable and it is not working correct just yet. I Im trying to find out why, it despite i shouting lower it keeps guessing one more time higher.

But be my guest and plunge into this code and see what you make of it.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include #include #include using namespace std; int main () { string HighLow; int xGuess, numberX; int userMaxRand=50, userLowRand=0; srand( time(0)); numberX=rand()%userMaxRand+userLowRand+1; int compMaxRand, compLowRand; compMaxRand = userMaxRand; compLowRand = compLowRand; userMaxRand = userMaxRand - userLowRand; compMaxRand = compMaxRand - compLowRand; xGuess=rand()%compMaxRand+compLowRand+1; cout << "Guess the number im thinking about between 1-50! Psst, the number is " << numberX << endl; cout << "I guess " << xGuess << ". Is it higher or lower? " ; while (numberX != xGuess) { getline (cin, HighLow); if (HighLow == ("higher")) { compLowRand = xGuess; cout << "Higher." << endl; } if (HighLow == ("lower")) { compMaxRand = xGuess; cout << "Lower." << endl; } xGuess=rand()%compMaxRand+compLowRand+1; cout << "I Guess " << xGuess << ". Is that correct?"; cout << " No, that is not correct." << endl; } return 0; }``````
Specifically to OP's question: capture the random number generation logic in a reusable function that is parameterized by the range of numbers you wish to produce. In this example, you can pass any two non-negative integers to the function generateRandomNumberInRange and it will return a random number in that range. Even if you pass the high end of the range before the low end, it will do the correct swap to handle this. So generateRandomNumberInRange(7, 20); will behave the same way as generateRandomNumberInRange(20, 7);
 ``1234567891011121314151617181920212223242526272829`` ``````#include #include #include unsigned int generateRandomNumberInRange(unsigned int lowEnd, unsigned int highEnd) { if(highEnd < lowEnd) { unsigned int tempForSwap = highEnd; highEnd = lowEnd; lowEnd = tempForSwap; } int rangeVal = (highEnd - lowEnd) + 1; return (std::rand() % rangeVal) + lowEnd; } int main() { std::srand(std::time(nullptr)); unsigned int randBetweenOneAndFifty = generateRandomNumberInRange(1, 50); std::cout << randBetweenOneAndFifty << std::endl; unsigned int randBetweenOneAndFifteen = generateRandomNumberInRange(1, 15); std::cout << randBetweenOneAndFifteen << std::endl; return 0; }``````
Last edited on
 ``12`` ``````unsigned int generateRandomNumberInRange(unsigned int lowEnd, unsigned int highEnd) {``````
`std::srand(std::time(nullptr));` [Error] 'nullptr' was not declared in this scope