# Thread: Randomly distribute people in groups

1. Member
Join Date
Jan 2014
Posts
44
Rep Power
0

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

2. ## 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

3. Member
Join Date
Jan 2014
Posts
44
Rep Power
0

## Re: Randomly distribute people in groups

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?

4. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
5

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

5. Member
Join Date
Jan 2014
Posts
44
Rep Power
0

## Re: Randomly distribute people in groups

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?

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,838
Rep Power
10

## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•