I've made a game where the user thinks of a number (not inputted) and the computer tries to guess the number. The user then tells the program if the number is too high, low or correct. I'm getting some undesired results during the game when I choose a high number (e.g. 99) and a low number (e.g. 1). I haven't done extensive testing as it's just a little excersize to test myself on a few things but the problems are getting are the program will sometimes repeat a guess twice, then often go down or up sequentially until it reaches the right number. On other occasions it will go down or up sequentially then jump to the extreme guess (1 or 100) then jump to a much lower/higher number. I'm thinking it's something to do with my random number equations but can't quite put my finger on it. If anyone has any ideas, please let me know.
What? This doesn't make any sense. time(0) (basically) returns the number of seconds since UTC midnight of January 1st, 1970, not a random number. And why is 'guess' part of the expression that generates the next number?
It's pseudo random, not technically random but it suits the purposes of the program. It just presents a seemingly random number by seeding rand to time. The guess is part of the expression generating the next number so that the program takes into account it's guess so that when the user inputs "high" or "low" it can calculate a new guess that is within the range of the previous guess and the maximum or minimum. It's a meesy program, I'll agree but it does the basic job it needs to do. I just wanted to see if it could be cleaned up using basic concepts as I'm a beginner and just setting myself a little excersize.
No, you are SEEDING the random function with time, which is good. But then you use the TIME instead of a random number. This is not pseudo-random, this is madness.
Apologies, simply overlooked a typo. Brain was in automatic. Edited the above code to display 'rand()' where it did say 'time(0)'. For some reason it still worked as a pseudo random number but besides the point. I still have the same issues after the minor edit and would still like a solution to prevent the stated issues.
Maybe if someone could explain a way of storing the values the program guesses and ensuring that if told it is higher or lower it won't pick a number higher or lower than that value for the rest of the round? Am I talking about arrays here? Not quite sure.
Thanks for the link, appreciated. Only problem is my program is working with even simpler concepts as I'm just revisiting some very basic stuff held within the first 2 chapters of the book I'm working with. Also, I don't have any variables in my program called low or high. The limit of 1 - 100 is set by the expression following the rand() function, the only thing it won't account for is when it is close to the guess, say the guess is 99 and it guesses 98, I will tell it that the guess is too low. It may then jump to 100, at which point I will call it out as being high and then it may jump right back down to 4 or some other low number. I need some sort of expression that will keep it within a range so that it recalls it's previous guesses, remembers they were too low/high and bases it's next guess on that basis. I was thinking I may need to store th guesses in an array and somehow tell the program to read it's contents and form a guess based on the data input for each value, i.e. high or low. Any tips on that front?
if (userCheck == "high")
{
ceil_limit = guess-2; // -1 to get between 0 and 99, another -1 to lower the ceiling by 1
guess = rand() % (ceil_limit - floor_limit) + floor_limit +1;
}
elseif (userCheck == "low")
{
floor_limit = guess; // -1 to get between 0 and 99, +1 to raise the floor by 1
guess = rand() % (ceil_limit - floor_limit) + floor_limit+1 ;
}
Initially, floor_limit is 0 and ceil_limit is 99.
EDIT: Add lieDetector(ceil_limit, floor_limit); between ceil_limit = / floor_limit = and guess = :
It looks good but when I tested it it gives me results above 100. On the first test the guess was 96, my answer was 99 so I input low and it gave me the next guess as 169 but with a little tweeking I'm sure I can get it to work.
I've tested it a few time with numbers 1 and 100, and it works okay for me
But I've had to add a conditional statement to the assignment, otherwise program will attempt to divide rand() by 0 if you have the guessing number cornered between two neighbor numbers (ceil and floor become equal).
I am new to C++ programming so my comments are modest. The book I am using to learn C++ had a similar program as an exercise. As I understood the problem and the program, you don't need to save prior guesses because they are used to set the new limits for the next guess. When the computer guesses and you reply that it is too high, then that guess is the upper bound for the remaining numbers from which to guess.
I am unable follow most of the code other posters wrote. I am pretty sure that you need to have some lines of the sort (in pseudocode)
if (guess is too high) upper_limit = guess
new guess = rand() * (upper_limit - lower_limit) + lower limit
if (guess is too low) lower_limit = guess
new guess = rand() * (upper_limit - lower_limit) + lower limit
I suggest you to try to do a lot of "exercise" programs like these, because you'll get "fit" quickly.
The formula works this way:
IF ceil minus floor isn't 0 (meaning there are still some numbers inbetween), THEN new guess is any number up to ceil minus floor (the difference), plus floor, to raise the "threshold" to the right place, plus one to move the numbers from "0-99" to "1-100".
ELSE ceil-floor is 0, ceil and floor are the same, which means that there are no numbers inbetween anymore, which means that either ceil or floor is the new number. Plus one to get into "1-100".
IF ceil is lower than floor (ceil-floor<0), THEN the user must be lying, because "threshold" was already searched and user said NO even to the "ceil-floor is 0" case.
(English isn't my first language, so I don't know any mathematical terms)