You are using a version without Ads of this website. Please, consider donating:

### rand() repeats same number

About fifty percent of the time my program will guess a number and repeat it until the loop closes. I have stared at this for a couple hours trying plenty of different things and I still for some reason cannot grasp the error of my ways. Thank-You in advanced for the help, I'm sure it will be a simple case of tunnel vision or something.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #include using namespace std; int guess_it(int high, int low); int main() { int guess; int high = 100; int low = 0; srand (time(NULL)); int number = rand() % 99 + 1; //cout << "Welcome to the guessing game!! \nThe number will be between 1 and 100.\nYou have 10 trys, good luck. \n\n"; //while(guess != number){ for(int trys = 0; trys <50 && guess != number; trys++){ /* cout << "Please enter your guess: "; cin>>guess;*/ guess = guess_it(high,low); cout<number){ high = guess; guess_it(high,low); cout<<"To high\n"; cout<<"Trys: "<
Comment the code at line 54.

`srand (time (NULL));`

It seeds random number generator to same number because your program execution time is less than thousands of milliseconds.

More over random number seeding is one time process for a program it is reliable.
Last edited on
The same error persists, but i do appreciate the input because i didn't think about that.
Perhaps if you actually used the value returned from the function, it would be more useful. As it is, you might as well not even call the function.

`guess = guess_it(high,low);`
Woud not ` guess = guess_it(high,low); ` assign the value returned, to the variable guess? Thus using the value returned by putting that variable into play? Just curious about what you mean exactly by that. Also is this what you meant by using the value returnd?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include using namespace std; int guess_it(int high, int low); int main() { int guess; int high = 100; int low = 0; //srand (time(NULL)); int number = rand() % 99 + 1; for(int trys = 0; trys <50 && guess_it(high,low) != number; trys++){ guess_it(high,low); cout<number){ high = guess_it(high,low); guess_it(high,low); cout<<"To high\n"; cout<<"Trys: "<

This is probably even worse coding that your eyes can fathom. But I am just learning and I appreciate all of the input.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include static void YouWin(int i, int guessnum); // If user won, they get an applause. static void YouWin(int i, int guessnum) { if(i>1) // Nudge them to play again { std::cout<<"\n\nWINNER! You guessed my number, "<>guess; if(guess>guessnum) { std::cout<<"Too high!\nTry again.\n\n"; //if their guess is too high, we say too high } else if(guess

DO NOT USE THIS CODE: 1. It is extensive and wordy. 2. It takes up too much memory. and time. USE THIS CODE ONLY AS AN EXAMPLE

Last edited on
 Woud not guess = guess_it(high,low); assign the value returned, to the variable guess? Thus using the value returned by putting that variable into play?

Yes.

 Also is this what you meant by using the value returnd?

No.

You actually had it correct. Well, except for the calls to guess_it that did nothing with the return value. Those should be removed. I didn't look closely enough at your original code, where you were actually using the return value in one place.

One thing that is wrong, though, is the code for guess_it. You're returning a value in the range low to high-1.

Should be:
 ``1234`` ``````int guess_it(int high, int low) { return rand() % (high - low + 1) + low; }``````

Thank-You, Cire and everyone else who fronted some input. I knew that it would be a simple mathematical error. I actually sat and tried to figure out what was wrong with it for a while. I must have gotten some serious tunnel vision or something. Thank-You though this:
 ``1234`` ``````int guess_it(int high, int low) { return rand() % (high - low + 1) + low; }``````

really helped and was the answer to my question.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: