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.
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.
#include <iostream>
#include <string>
#include<conio.h>
#include<stdlib.h>
usingnamespace 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;
}
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
usingnamespace 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;
}
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <ctime>
usingnamespace 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 " <<xRan;
}
if (HIGHLOW == ("lower"))
{
xRan=rand()%xRan-50;
cout <<" I am guessing " <<xRan;
}
return 0;
}
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?
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
1 2 3 4
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 =)
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace 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: ";
elseif (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;
}
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.
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);
Btw, what kind of system are you on? I tried the code in Orwell Dev C++, but reports that std::srand(std::time(nullptr)); [Error] 'nullptr' was not declared in this scope