Random number Gen

SO..I'm reading through a beginners code book and one of the exercises they have us do is have the Compiler generate a random number, and using a loop, we have to input new guesses until the number is found.

Got that one OK, at the end of the chapter, they challenge us to create the opposite: set a number, and using a number gen have the Compiler generate numbers until it is found.

the problem I'm having is that it keeps outputting the same number (I had to add a "break;" to stop the infinite loops)

QUESTION - how do i get it to generate a new number when the "getNum()" function is called? (I'm uber n00b, so there might be another way, this way just made sense to me....)

BONUS second question - Why is it always saying the value is too low?

Any help is greatly appreciated!

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int guess,target;

int getNum(){
    
    srand(static_cast<unsigned int>(time(0)));    //seed random number generator
	int guess = rand() % 100 + 1;        // random number between 1 and 100
	cout<<"My guess is: "<<guess<<"\n"; 
    
	return 0;
}

int main()
{
    cout<<"Target Number - ";
    cin>>target;
    getNum();
    do{
        if(guess > target){
            cout<<"Too High...\n";
            srand (time(NULL));   //attempt to clear the srand value....
            getNum();
            break;      //Damn infinite loops....
        }
        else if(guess < target){
            cout<<"Too Low...\n";
            srand (time(NULL));
            getNum();
            break;
        }
        else{
            cout<<"YOU GUESSED IT!!!";
        }
        
    
    }while(guess != target);
    
}


Output:
1
2
3
4
Target Number - 26
My guess is: 91
Too Low...
My guess is: 91
Read the comments, then see what you can do:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int guess,target;

int getNum(){
	
	srand(static_cast<unsigned int>(time(0)));   // only need this once
		//reseeding srand with time(0) only changes the result of rand() once per second
	guess = rand() % 100 + 1;	// declaration of guess here overrides global declaration
						// so this doesn't put anything into global int guess

	cout<<"My guess is: "<<guess<<"\n";
	return 0;
}

int main()
{	
	cout<<"Target Number - ";
	cin>>target;
	getNum();
	do
	{
		if(guess > target)	// when it gets here, global guess is still zero.
		{
			cout<<"Too High...\n";
			srand (time(NULL));   //don't need this in here
			getNum();
			break;      //infinite loop because guess is never > target
		}
		else if(guess < target)
		{
			cout<<"Too Low...\n";
			srand (time(NULL)); // don't need this here
			getNum();
			break;  //infinite loop because global guess is always 0, < target
		}
		else
		{
			cout<<"YOU GUESSED IT!!!";
		}
	}	while(guess != target);
	system("Pause");
}
Last edited on
OK, that all makes sense, but if i comment out the breaks, it still loops with the same number.

So......I'm thinking i should not use time as the seed, seeing as it only updates 1/sec.....

Or is there a way to get it to regenerate every time its called?

Here is what i have so far:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int guess,target;

int getNum(){
	
	srand(static_cast<unsigned int>(time(0)));   //needs to generate a new number when called
	guess = rand() % 100 + 1;
	cout<<"My guess is: "<<guess<<"\n";
	return 0;
}

int main()
{	
	cout<<"Target Number - ";
	cin>>target;
	getNum();
	do
	{
		if(guess > target)	
		{
			cout<<"Too High...\n";
			getNum();
			//break;      
		}
		else if(guess < target)
		{
			cout<<"Too Low...\n";
			getNum();
			//break;  //infinite loop because global guess is always 0, < target
		}
		else
		{
			cout<<"YOU GUESSED IT!!!";
		}
	}	while(guess != target);
	system("Pause"); //supposed to stop and wait for input??
}
Last edited on
@PCrumley48 - Line 13 is not a declaration. It is an assignment. It does in fact change the global.

@OP - Using time() as an argument to srand() is fine. The point is your call to srand() is in the wrong place. srand() should be called ONCE. Move the call to srand() to line 18. Calling srand() multiple times within the same second will reset the RNG to the same sequence of numbers causing the RNG to return the same number each time it is called.

A word of advice. If you're going to try and guess a number, you want to make your getNum a little smarter. You should keep track of the highest and lowest number guessed. You should then generate a random number between between high and low. As your code is now, if you report the guess is too high, you will still possibly generate random numbers higher than that guess, which is pointless.
Last edited on
PERFECT!!!

That fixed it, thanks for the help folks!
Abstraction: In his original code, the line 11 said int guess = , which makes guess local to the function.

I had cut, pasted into VS2012, edited to make it work, and forgot that I had made some changes before pasting it back.
My bad. I was looking at your line 13, thinking it was his.
My bad. I was looking at your line 13, thinking it was his.


Well, it was as much my bad, too. I wanted him to see how the situation was causing the zero to be seen in main(). . .and the guess always being too low. Oh, well, "we can't all. . ."
Topic archived. No new replies allowed.