Results 1 to 6 of 6
  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Randomly distribute people in groups

    Quote Originally Posted by JosAH View Post
    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. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default 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. #5
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Randomly distribute people in groups

    Quote Originally Posted by pj6444 View Post
    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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,781
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Mather groups order
    By jammybell in forum New To Java
    Replies: 1
    Last Post: 04-08-2012, 06:52 PM
  2. Groups of swing objects
    By plrca2 in forum AWT / Swing
    Replies: 12
    Last Post: 12-07-2010, 02:56 AM
  3. How to distribute own software in Netbeans?
    By Juarez in forum NetBeans
    Replies: 17
    Last Post: 05-04-2010, 04:43 AM

Posting Permissions

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