Random Number Assign

I know that you can generate random numbers using the "srand ( time(NULL) );" but im curious if there is a way to randomly generate numbers with a cap. For example I want to assign a group of 6 people random numbers between 1 and 3. I don't want the numbers to repeat more then twice. So it would create an even amount of pairs. Is there anything that could help me with this?
Ideas:

You could write a function that uses rand and checks that the values only occur at most twice as it generates them.

You could create a function object that does the above and use the STL generate algorithm. (this one is more cool)
Another idea:
If you are assigning the numbers 1-3 to 6 people and no number more often than 2 times, every number will be chosen 2 times. So you can choose the people and assign them to the numbers.

Yet another idea:
This doesn't work if the range of numbers and/or the number of people vary. So another idea: If you have got n numbers, make a list out of 2*n elements containing every number twice. Then you can create a random number between 0 and 2*n-1, get that Element of the list and delete it afterwards. Then reace a random number between 0 and 2*n-2 and so on... This hasn't got the same probabilities (the probability to draw a new number is twice the probability to draw a number, that is already assigned), but it could match your problem.
Seymore that was one of the ways I was thinking of doing it. I don't think I know how to do the second way.
Mordekai I need to have random pairs each time the program is executed. And wouldn't the second solution almost never generate the same number twice?
The probability it draw a new number is twice the probability to draw a number, that has already been drawn, so it is not "almost never". It seems to me like every number should be drawn twice (to generate pairs). In this case, if you have got a list of n people, you can randomly pcik one, give him the number 1, pick another one from the remaining n-1 people and also give him the number one. Then you can pick one from the n-2 people and give him the number 2, and so on... i think this would be the simplest and best solution to generate pairs.
Topic archived. No new replies allowed.