Results 1 to 7 of 7
  1. #1
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Question Shuffle Group Method

    Hello!

    I am pretty new to Java and I am currently trying to write a group dividing program.
    However, I have faced a major problem that needs to be solved in order to make the program work the way I want.

    To simplify the problem, let say that I have got 6 person objects that consists of a name and a person index (0-5).
    I want to make three groups with two persons in each group (randomizing witch persons are paired with each other). This is simple and I know how to do it.

    Then comes the tricky part. I want to make new groups out of the same persons, with the condition that no person is placed in the group with the same partner as the previous time. And then I want to do it a third time, with three new unique groups. Etcetra. When it is not possible to make new unique groups, I want to get some kind of message.

    Do you have any ideas how to do this?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Interesting...

    The first thing that popped into my mind was a map.
    • Assign each person a number (1-6).
    • When you create the initial pairs, assign numbers to the pairs, for example, 16, 25 & 34.
    • Reverse each number, 61, 52 & 43 and put these 6 numbers in an arraylist. Form the pairs again and check newly formed numbers against the numbers in the arraylist.
    • If repeated, form the pairs again.
    • If not, save the new 6 pairs in the arraylist.
    • I think when the arraylist has 30 elements, you're finished (don't trust this number... see disclaimer below).


    Disclaimer: I have a terrible head code and bronchitis. I'm spaced out on cough syrup. Use at your own risk. Probably somebody with a clear head and really knows how to program can suggest something neat like maps, hashmaps, etc.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default This is what I did ...

    Not sure if this is what you're looking for....
    What I did is get all the possible combinations of couples without repetition. It turns out that that is 15 couples (my last post was wrong: it's 15, not 30... I'm blaming the cough syrup).
    This is what I did:
    • Used a HashTable for the people (6 entries)
    • Used an ArrayList for the combination of couples (15 elements)
    • Generate two random numbers (1-6), making sure that they're not the same and create the first couple (random1= 5, random2=3, couple =53)
    • reverse the numbers of the couple (reverseCouple = 35)
    • add the couple number to arraylist if couple and reveseCouple aren't already in the arraylist
    • Repeat...

    Hope this helps...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default My solution

    The mine is a "collection" solution
    Java Code:
    import java.util.*;
    
    public class Person {
       //Use this field into some method to create unique ids
       //Of course, you can use a for loop when you need more ids
       //and you MUST have a getter method to use it
       private static int[] availableIds = { 0, 1, 2, 3, 4, 5 };
    
       //Now define this using the above minus this person's id
       private Set<Integer> notYetAFriend = ...
      
       //This returns the array (because we know its size) of the 
       //elements in notYetAFriend
       public int[] getNeverFriends() { ... }
       
       //And this removes from notYetAFriend the spec id
       public void removeNeverFriends(int id) { ... }
    
       //This is a flag to see if your Person NOW have a friend
       //You can have getter and setter methods if you want to
       //use encapsulation
       private boolean hasFriend = ...
          
    }
    Then you have a class named Party (eh eh :D) with a makeCouples() which uses the availableIds field in a for loop.
    First check that notYetAFriend is not empty, then use the flag hasFriend and getNeverFriend() to choose if your Person needs a companion and who he can be.
    NOTE
    The CJLSMAN's solution is much more compact and quicker, but if someday you decide to use something different for an int in the range [0-6) for your ids (for example an hexadecimal number or a String obj) it can be quite difficult to implement

  5. #5
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks so much for your posts!

    However, I think that maybe I have to explain the complicated version of the programs function in order for you to give the best advices.

    I wont be working with 6 persons in the final app, but with a school class with somewhere between 15 and 45 students. The groups created will consist of 4 or 5 students depending on the total number of students in the class (15 students equals three 5-member-groups, and 16 students four 4-member-groups, etc). I have written a method that decides how many groups of witch type you need after typing the total nbr of students.

    I will need to create six different group combinations for each class (two each year for three years), and some students will eventually have to be together with a student that they already have worked with in the past. The thing is that I want to create as unique groups as possible every time. There does also have to be possible to add and remove students from the class during the years.

    To clarify, I don't know how many student each class consists of, that is something the teachers have to type in when they create the class.

    Feel free to ask questions if there is something you need to know or if you don't understand my English (I'm from Sweden)...:)

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default uh...

    xelo... it would appear that you're expecting somebody to solve all the requirements for you and give you a solution (please correct me if I'm wrong). There have been various suggestions on how to tackle this task. I think it's your turn to take the suggestions and start to implement your own code, with the specific requirements. If you you get stuck, the forum will gladly offer more help and suggestions.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Of course I don't expect anyone to write down a working code word by word, and I have got a few ideas to work with from you guys which I really appreciate.

    I just thought it would give you some new thoughts if I posted the whole problem. For example, your idea CJSLMAN of making a number of each group (f.e. 35) wont work with more members in each group, if I have got a group "called" 1547, I do not want to get a new group later called 1548, it is a new unique one, but three of the members have already worked with each other...
    Do you see my issue?

    However, thank you so much for your help!

Similar Threads

  1. 2D Array Random Shuffle
    By Nuluvius in forum New To Java
    Replies: 8
    Last Post: 11-23-2008, 06:15 PM
  2. How do I shuffle an arraylist?
    By frasifrasi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 11:29 PM
  3. New To The Group
    By javarishi in forum Introductions
    Replies: 5
    Last Post: 04-10-2008, 04:34 AM
  4. How to Shuffle a particular String
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-06-2008, 07:38 PM

Posting Permissions

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