Thread: Random Selection - Tabu List

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18

    Random Selection - Tabu List

    Hey guys,

    So my current problem, after great simplifications boils down to this:

    Lets say you have a 100 numbers, and I want to pick a random one. If its not 'right' I need to pick another one and continue until a certain success criteria is met (irrelevant what that might be).

    My question is how can I fairly efficiently implement it. For example if I'm at a state where I already tried say , 5 and 8. I still have 98 possible numbers to try, but not 5 and 8. How can I accomplish this in c?

    [of course I can just always pick a random number 1-100, and keep track of what I already tried, iterate though that list, and if I already tried it, just try picking a new number, however the inefficiency of this will be unacceptable for my needs, I'm afraid. This would only be acceptable (but still ugly) if the success criteria was easy to meet, but would be terribly inefficient when it wouldn't]
    Last edited by otaconBot; 08-15-2012 at 06:50 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Keep an array of 100 numbers 1 to 100.
    When you pick a random number, say 5, you grab the number in position [5] (which is 5 initially). To make this number unselectable, just substitute the number in the highest slot (which is 100 now) into it. Decrement "highest" to 99.
    Next random choice is limited to 99 positions.
    Next time if you happen to choose slot [5] again you'll receive 100 instead.
    Each time decrement number of choices of the random generator.

    Of course choose indexing 0 to 99 and appropriate numbers and add 1 if you want 1 to 100 range.
    Last edited by nonoob; 08-15-2012 at 07:01 AM.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18
    /me hugs nonoob

    Elegant and efficient Exactly what I needed! That was quick, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random structure selection problem
    By Baron.Slay in forum C Programming
    Replies: 3
    Last Post: 06-25-2012, 07:51 PM
  2. Botched random choice selection function
    By Xom in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2011, 01:03 PM
  3. Random selection of a word
    By doobyscoo in forum C Programming
    Replies: 1
    Last Post: 04-14-2003, 11:39 AM
  4. random number selection
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 09:02 AM
  5. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 12:59 AM

Tags for this Thread