Random Number

Ok I am have a simple issue that is eluding me badly for some reason I am running a simple loop to get a random num generator to guess 10 different numbers but it keeps guessing the exact same number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int _tmain(int argc, _TCHAR* argv[])
{
	srand(static_cast<unsigned int> (time(0)));

	int randomNumber = rand();
	int guess, compGuess  = (randomNumber % 10) + 1;;
	
	

	

	int tries = 0;
	

	do{
		
		
	
		cout << compGuess;
		++tries;
		
	} while (tries < 10);
This actually made me laugh. If you want it to guess, different numbers each time then you need to tell it to do so, by doing what you did on lines 5 and 6, again in the loop
Why made you laugh? Also I put this

compGuess = (randomNumber % 10) + 1;

in the end of the loop and it still is not doing it

Ahh I see I need this in it

int randomNumber = rand();

Can you tell me why I need that in the loop ?
rand() is the one actually generating random numbers. That is why I thought it was funny that you had the actual random number generator outside the loop
lol man what seemed like such an easy code turned out to bit me in the A*s I got one more question if you don't mine I am doing the have the computer guess my number game and I have this

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
int _tmain(int argc, _TCHAR* argv[])
{
	srand(static_cast<unsigned int> (time(0)));

	int guess;
	int tries = 0;
	int compGuess;
	cout << "Please tell us your guess\n";
	cin >> guess;

	do{
		int randomNumber = rand();
		compGuess = (randomNumber % 10) + 1;

		if (compGuess < guess){
			cout << "comp is to low\n";
		}
		else if (compGuess > guess){
			cout << "comp is to high\n";
		}
		else{
			cout << " the computer guessed correctly with " << compGuess << endl;
			
		}
		
		
	} while (compGuess != guess);



It seems like its guessing it every single time. I don't know if I am burning myself out or not but it seems like I can't figure out something like this looks ridiculous on me lol
Ahhh I think I figured it out I reversed the while order and made it guess != compGuess, not sure I understand why that made it work though
Ok nice, but that should not have caused what you said was happening
See I was wondering why it would of caused it, you think you can run the one I posted up and see if it does it for you? cuz after I changed the while loop it worked but it makes no sense to me what so ever lol
I ran it and it just kept saying comp is to low, so I killed it.
Topic archived. No new replies allowed.