Randomly distribute people in groups

• 01-18-2014, 04:10 PM
Stud1
Randomly distribute people in groups
I want to create a program that takes n number of people (String vector) and assigns them a random group number (int) the range from 1 to k. The number n is evenly divided by k.

I have created a program that associates every String with a random int number. There is however the problem that the random generator just gives a random number, but doesn't make an equal number of people for each group. Which means even if the numbers are random, some of the numbers happen to come up more times than others.

How can I write, to make sure that each number is generated the same amount of times?
• 01-18-2014, 05:07 PM
JosAH
Re: Randomly distribute people in groups
Use the Collections.shuffle( ... ) method to shuffle the List of people; next, assign them a group number in order, i.e. 1,2,3 ... k, 1, 2, 3 ... k etc.

kind regards,

Jos
• 01-18-2014, 08:57 PM
Stud1
Re: Randomly distribute people in groups
Quote:

Originally Posted by JosAH
Use the Collections.shuffle( ... ) method to shuffle the List of people; next, assign them a group number in order, i.e. 1,2,3 ... k, 1, 2, 3 ... k etc.

kind regards,

Jos

Thanks, I will read up on the Collections class! However I wonder if it's also possible to do with the Random class? And if so, how?
• 01-18-2014, 10:10 PM
pj6444
Re: Randomly distribute people in groups
What are you going to do if n = 17 and k = 2? You will have one person left over.
• 01-18-2014, 10:19 PM
Stud1
Re: Randomly distribute people in groups
Quote:

Originally Posted by pj6444
What are you going to do if n = 17 and k = 2? You will have one person left over.

In the case I'm using it will be so that n is evenly divided by k, like I wrote. So how can it be done?
• 01-19-2014, 12:52 AM
jim829
Re: Randomly distribute people in groups
You have already been told how to do it. As you profoundly stated, "there is however the problem that the random generator just gives a random number", so you can't count that a small sample will be evenly distributed. Flipping a coin is 50/50 (baring an edge outcome) but you can still flip six tails (or heads - US coinage) in a row. If you want to shuffle the list of folks yourself and not use Collections then do a web search on shuffle algorithms. The algorithms are well documented.

Regards,
Jim