1. ## random number checking...

Hello everyone,
Curiously, I wrote a program that you give the computer a number to guess and it loops through random numbers...if the computer finds a match it prints out a message and whatnot. The only problem is, it repeats numbers....so sadly out of 100 possible choices...it could take 394 tries...something which is not cool. So my question is, is it possible (using some built in function perhaps) to have the comp check its newly generated random number with ALL the ones it has called previously, if there's a match, generate new number and so on...if it doesn't match any previously called, use it and so on...Curiously, what if I used an array of all possible choices, is that in the right direction? Any insight into this would be fantastic. Thanks a lot!-Chap

2. You could use a set container to store the list of generated random numbers. A set only stores unique values (no duplicates). Each time you generate a new number, you try to insert it into the set. One of the versions of the insert member function returns a pair object consisting of an iterator (the first pair member) and a bool (the second pair member). The bool determines whether the insert call was a success (true) or not (false) and therefore whether or not we have already encountered the value we are trying to insert.

Code:
```set<int> intSet;
int value;
...
value = rand()%20;
while( !(intSet.insert(value).second) )  // While insert into set fails (value already exists)
value = rand()%20;                   // Generate new random to try and insert

// Once we get here, we know random number stored in "value" has not been encountered before.```
I would however suggest you do something such as keeping track of the upper and lower bounds your computer has to check and generate your random number within those bounds (inclusive). Your program would have to know if its guess was high or low (or right on) using this method and adjust the bounds appropriately. As an example:

Try to guess number 56 for example.
Lower Bound - 1, Upper Bound = 100.
Generate random number between 1 and 100 (inclusive) maybe 32 this time around.
You determine that this guess is too low and adjust lower bound appropriately.
Lower Bound - 33, Upper Bound = 100.
Generate random number between 33 and 100, say 87 this time.
You determine this number is too high and adjust upper bound appropriately.
Lower Bound - 33, Upper Bound = 86.
Process repeats until we have a match.

Doing something like that would eliminate the need to check for repeated randoms but may not exactly be what you need if your program is to have no help at all in determining if a guess is high or low.

3. Really, the best way to do this (if I understood you correctly) is just the start at the minimum possible number and increment up to the maximum.

Alternatively if what you want is for the computer to guess randomly, then you could use an array with as many elements as there are possible guessed, and "tick them off" like with a boolean, then refer to the corresponding array element when you generate a guess, and check whether it has already been guessed.

I didn't really understand what you were asking, so I could be on the wrong track.