# Thread: specific rand() issue

1. ## 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. 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. 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. 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?

5. Originally Posted by sy84n
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. 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.

7. Originally Posted by brewbuck
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.

Popular pages Recent additions