Results 1 to 6 of 6
 01182014, 04:10 PM #1Member
 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?
 01182014, 05:07 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,904
 Blog Entries
 7
 Rep Power
 22
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,
Joscenosillicaphobia: the fear for an empty beer glass
 01182014, 08:57 PM #3Member
 Join Date
 Jan 2014
 Posts
 44
 Rep Power
 0
 01182014, 10:10 PM #4Senior Member
 Join Date
 Jan 2013
 Posts
 166
 Rep Power
 3
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.
 01182014, 10:19 PM #5Member
 Join Date
 Jan 2014
 Posts
 44
 Rep Power
 0
 01192014, 12:52 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,425
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Mather groups order
By jammybell in forum New To JavaReplies: 1Last Post: 04082012, 06:52 PM 
Groups of swing objects
By plrca2 in forum AWT / SwingReplies: 12Last Post: 12072010, 03:56 AM 
How to distribute own software in Netbeans?
By Juarez in forum NetBeansReplies: 17Last Post: 05042010, 04:43 AM
Bookmarks