Thread: specific rand() issue

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    specific rand() issue

    Allright, So in continues to some replies that have been I see I eventually need to open a new thread.

    I already read the topic about the rand() in the FAQ and couldn't get the answer I was looking for. I also didn't get the answer in the replies I got. Although I got great answers that did the job when you don't use scanf. When I tried applying it into the program I want to create - it did not work well.

    I'm trying to create a program that will run several weight categories.
    Each category needs to have its own weight approximations. (between 'x' to 'y')

    Lets say we have only 2 categories. In each category there are 3 men and 3 women.

    In category A, the approximated weight is between 50-80.
    In category B, the approximated weight is between 81-100.

    The user will need to enter one of the categories and choose 1 men and 1 woman.
    Before he chooses he will see their weight. (Their weight needs to be random, within the category randomized limits that were set earlier)

    Now after the user have chosen, lets say he needs to choose again from the same category...

    I want to make sure that the other 4 people that remained in that category he entered earlier, won't necessary have the same weight again, but different weights - within the category randomized limits that were set earlier. So when next time he enters that category - it will not be exactly the same. it doesn't need to be completely different, after all we're talking about random here, but it does need to be abit different.

    I think my main problem is to create a loop that will randomize the people weights within the category.

    I just want to say thanks to dwks & iMalc who did help me understanding the rand() process, I guess I should have explained myself better from the start.

    Hope somebody could help me with this one.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It seems like you know how to get rand to give you something in a specific range, so do that.

    You will have to keep generating new weights until each person has a unique weight. Keep the weights that satisfy the range and uniqueness requirement so far in an array, and do a linear search to check as you handle each new person's weight.

    This approach would work well if you decided to use user input as well, instead of a PRNG.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Unless you allow for the exact same state to happen again, by random chance, then you are interfering with the randomness.

    You get random by letting it be random, not by manipulating it to make it LOOK random.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    are you positive on that?
    I mean... there must be some kind of way to program the random behavior.

    like... (in words): "random but the next time you random don't repeat at least 1 (if not even more) of the results you gave the last time."
    edit: adding to the formula (in words): "but if you do repeat all of them, skip it and do random again until it's optimal"

    Look, I don't mind that for 3 out of 4 people that remained - it will be exactly the same in the results, but atleast 1 of them oughta be different. can't that be arranged somehow?
    Last edited by sy84n; 03-31-2008 at 11:22 AM.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sy84n View Post
    Look, I don't mind that for 3 out of 4 people that remained - it will be exactly the same in the results, but atleast 1 of them oughta be different. can't that be arranged somehow?
    The only way to avoid a repeat is to track every configuration and make sure you never repeat it. There is no way to "program" the rand() function to do that for you.

    And it won't be random anymore.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unless you allow for the exact same state to happen again, by random chance, then you are interfering with the randomness.
    ...
    And it won't be random anymore.
    I am not so sure about that. Looking at it from another angle, sy84n is asking about how to select a few numbered cards at random from a bag without replacement. The bag contains cards with distinct numbers. That there is no replacement does not mean the selection is not random.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Quote Originally Posted by brewbuck View Post
    The only way to avoid a repeat is to track every configuration
    You don't need every configuration. You need only the last one that was. The ideal is that it won't repeat itself after what it was 1 time before.

    example:
    1st round - 1, 2, 3, 4
    2nd round - 1, 2, 6, 4 (result = different from the 1st time in only 1 case)
    3rd round - 2, 2, 6, 4 (result = different from the 2nd time in only 1 case)
    4th round - 1, 3, 4, 2 (result = exactly like the 1st time but different from the 3rd time)


    Regarding to the cards idea,
    well it's not exactly like that, there's an imagination but a big change.

    like say that in the 1st round (above), you choosed #2.
    so.. in the 2nd round, #2 should not appear because it is now out of the cards bag.
    BUT, in that same 2nd round, #1 or #3 or #4 should not appear, because as said earlier - at least one of them should not be in the bag now.

    I know how it sounds... like it's a rigged card game
    but seriously speaking now, it's not supposed to be a card game in the first place.
    I don't want to get into specifics right now, but lets just say I will be really amazed if there's no solution for that.

    I'm not really asking to program the random function itself.
    I'm asking to program it's results, after he gave them.
    so when he gives me the results I do not want - it will re-random until he applies the program I made for the upcoming results.

    the main problem on that is that when it will re-random, it will be based on the bad results he gave earlier, meaning I might do get the numbers that were already there, before "I" asked to re-random.

    If you're confused by now.. don't worry, took me some time myself to get it straight


    to make it clear:

    1st round = bad result.
    2nd round = re-random based on 1st round's bad result, don't give that result again. = different result.
    3rd round = bad results (meaning the same bad results on the 1st round). << The random "will think" this is a good result because as far as "it" knows, he didn't gave the same result as in the last 2nd round, but he did gave the bad result of the 1st round without knowing he did.

    It's a messy messy challenge, I know.
    Last edited by sy84n; 03-31-2008 at 01:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform specific API or C standard API
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 01:32 AM
  2. How to list specific files in terminal
    By mikeyb in forum Tech Board
    Replies: 3
    Last Post: 10-25-2006, 10:10 AM
  3. Specific Keys in C, getting and putting
    By Axpen in forum C Programming
    Replies: 17
    Last Post: 01-08-2004, 09:54 PM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM
  5. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM