Thread: random number checking...

  1. #1
    Registered User
    Join Date
    Sep 2004

    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 sadly out of 100 possible 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. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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.

    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.
    Last edited by hk_mp5kpdw; 01-11-2005 at 12:41 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    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.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM