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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdlib>
#include <ctime>

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<<guess<<"\n";

    if(guess<number){
            low = guess;
        guess_it(high,low);
        cout<<"To low\n";
        cout<<"Trys: "<<trys<<endl;
    }

    else if(guess>number){
        high = guess;
        guess_it(high,low);
        cout<<"To high\n";
        cout<<"Trys: "<<trys<<endl;
    }

    else{
       cout<<"You guessed it! The number was "<<number;
    }


    }

}

int guess_it(int high, int low)
{
    srand (time (NULL));
    return rand() % (high - low) + low;



}
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>
#include <ctime>

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<<guess_it(high,low)<<"\n";

    if(guess_it(high,low)<number){
            low = guess_it(high,low);
        guess_it(high,low);
        cout<<"To low\n";
        cout<<"Trys: "<<trys<<endl;
    }

    else if(guess_it(high,low)>number){
        high = guess_it(high,low);
        guess_it(high,low);
        cout<<"To high\n";
        cout<<"Trys: "<<trys<<endl;
    }

    else{
       cout<<"You guessed it! The number was "<<number;
    }


    }

}

int guess_it(int high, int low)
{
    //srand (time (NULL));
    return rand() % (high - low) + low;



}


This is probably even worse coding that your eyes can fathom. But I am just learning and I appreciate all of the input.
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <ctime>
#include <cstdlib>

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, "<<guessnum<<", in "<<i<<" moves! Nice! Try to guess in "<<i-1<<" moves!";
	}
	else // If they somehow guessed the number in 1 move, they get praised.
	{
		std::cout<<"\n\nPRO! You guessed my number, "<<guessnum<<", in "<<i<<" moves! Genius! Either you are a magician or you are a mind-reader!";
	}
}

main()
{
	int guess,guessnum,i;
	bool win;
	win=false; //initialize checking value
	
	std::cout<<"This is a number guessing game!\n";
	std::cout<<"I will randomly choose a number between 100 and 0 and you will have to guess it in 10 guesses.\n";   //prompts user
	std::cout<<"Okay, lets begin!";
	std::cin.ignore();
	std::cout<<"\n\nThinking of a number...";
	srand(time(NULL));                                    //gets number
	guessnum=rand()% 99 + 1;
	std::cout<<"\nThought of a number!\n\n";
	for(i=1;i<11;i++)
	{
		std::cout<<"Guess Number "<<i<<":  ";      //prompts user to guess a number (includes turn)
		std::cin>>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<guessnum)
		{
			std::cout<<"Too low!\nTry again.\n\n";      //vice-versa if their guess it too low, we say too low
		}
		else
		{
			YouWin(i,guessnum);              //otherwise, they guessed it and we praise them, check that they won, and break from the loop
			win=true;
			break;
		}
	}
	if(i==10 && win==false)                     // if they lose (win==false), and their turns are over (i==10), we prompt them pur number and wish them luck
	{
		std::cout<<"You did not guess my number in 10 moves! The number was "<<guessnum<<". Better luck next time!";
	}
}


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

EDIT 11:02 AM : Added comments
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:
1
2
3
4
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:
1
2
3
4
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.