Random number generator help for a noob

Started by
8 comments, last by DevFred 14 years, 10 months ago
Ok so I'm self teaching myself c++ and using Bloodsheds dev c++ ide. Yes, I know about visual c++ still not sure how I feel about it. Anyways working on a text guess my number game, typical end chapter exercise. I'm happy with the way its seemed to turn out except for the random number generator. I want to make the range go up and down depending on what the computer guesses. Instead I get the same number twice before the number changes with no change in the range whatsoever. Any help/advice on my code would be greatly appreciated, also critisism or changes that need to be made that will make me a better programmer in the end. Thank you! //guess the number game 2.0 #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int tries = 0; cout << "\tWelcome to Guess my Number\n"; cout << "\tPick a number from 1 - 100 and see if you can stump the computer\n"; cout << "\tEnter symbol if the number is Higher(+), Lower(-), Correct(=)"; char playerAnswer; do { srand(time(0)); int numMin = 0, numMax = 100; int compGuess = rand() % (numMax - numMin) + 1; cout << "\n\nOk then my guess is: " << compGuess << "\n" << endl; ++tries; cout << "Is that correct?: "; cin >> playerAnswer; if (playerAnswer == '+') //if computer needs to guess higher { numMin = compGuess; //tryed to change the random number range here does not work cout << "\n\nOk then my next guess is: " << compGuess << "\n" << endl; ++tries; cout << "Is that correct?: "; cin >> playerAnswer; } if (playerAnswer == '-') //if computer needs to guess lower { numMax = compGuess; //tryed to change the random number range here does not work cout << "\n\nOk then my next guess is: " << compGuess << "\n" << endl; ++tries; cout << "Is that correct?: "; cin >> playerAnswer; } if (playerAnswer == '=') { cout << "\n\nYes I got it! In " << tries << " tries" << endl; } } while (playerAnswer != '='); system("pause"); return 0; }
Advertisement
You should only call srand(time(0)); once in your program.
http://www.jasonbadams.net/20081218/why-you-shouldnt-use-dev-c/
reading material for you

as for the random number,

first off, do not put srand() in the loop itself, you only need to call this once and only should you call it once.

just put it at the top of your main fn.

also if you get a warning on conversion from time() for the srand()
try a static_cast<int>

--ninja'd
Learn to use your debugger and place watches on compGuess and you will see the differences of seeding the random number generator once outside your loop as is normally done vs how you did it :)
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
yeah, I did originally have it seed up top. I guess I just didnt think about it when I cut and pasted a few sections of my code. Also after the reading sounds like I'm gonna have to change my IDE to deal with debugging anyways.
oh, and not to force you to use Microsoft's Visual C++, and I can't really explain why you should switch to it. Other people, on the other hand, can and will tell you.

Long story short:
lose Bloodshed Dev C++, get Microsoft Visual C++
A great IDE is Code::Blocks. You can use it with Microsoft's compiler or with the MinGW compiler.
The next step is to make the computer use the '+'/'-' information to educate the next guess. :)
First the rand() function returns a random number between 0 and 1. The % sign makes the range increase. The expression % 7 for example would be a number between 0 and 6. The last number advances the minmum number of the range by 1. So, what you need to do is add the minimum number variable(numMin) to the end instead of the number 1, this way the lower end of the range will move up when it is needed instead of always being 1.

And yes, you do need to call the rand(0 function each time you make a new guess.

int compGuess = rand() % (numMax - numMin) + numMin;

Then later, you do ask the program to print out the same guessed number.

****
numMin = compGuess; //tryed to change the random number range here does not work
cout << "\n\nOk then my next guess is: " << compGuess << "\n" << endl;
****

As you do not change the compGuess variable with any statement. So, you need to add another line as above before the next guess. Otherwise the variable compGuess remains the same as before.

****

numMin = compGuess; //tryed to change the random number range here does not work
int compGuess = rand() % (numMax - numMin) + numMin;
cout << "\n\nOk then my next guess is: " << compGuess << "\n" << endl;
****

The other thing that I see is that you have the program printout a guess twice each loop, so actually you can remove the second printout altogether and just change the range and return to the top.

The general flow would be as follows:

1. Make a guess, ie: set the variable compGuess using the rand() function.
2. Display the guess.
3. Ask the user if it is higher, lower or correct.
4. If lower subtract from the maximum, if higher add to the minimum. ie: set one of the range variables (numMin, numMax) to the guessed number.
5. Go to top of loop if the answer was not correct.

It is always very crucial to map out the general flow, ie: flow chart it to formulate the flow of logic in your mind. You do not have to be fancy about it. Some people use a psuedo programming language, some use flow chart programs and fancy symbols. I just use general statements that tell me what is needed.

Hope that helps.

John Barradale
Gorrideon Software
After 30 years of programming I hope that I learned at least SOMETHING.
Quote:Original post by Scharfschuetze
First the rand() function returns a random number between 0 and 1.

Wrong. rand() returns a random number between 0 and RAND_MAX. A typical value for RAND_MAX ist 32767.

Quote:Original post by Scharfschuetze
The % sign makes the range increase.

The % operator makes it DECREASE. Instead of 32768 randum numbers, rand() % 6 only yields 6 different values.

This topic is closed to new replies.

Advertisement