samedi 2 mai 2020

Randomly distribute people in groups

I am working on a group generator and using this method to group people

public String nMix(String file, int numOfGroups) {
   ReadFile info = new ReadFile();
   ArrayList<String> studentInfo = info.readEachWord(file);

   List<PeopleClass> people = new ArrayList<PeopleClass>();
   for (int i = 0; i < studentInfo.size(); i += 4) {
      people.add(new PeopleClass(studentInfo.get(i))); //name
   }

   Collections.shuffle(people);
// System.out.println(people.get(0).getName());

   Function<PeopleClass, String> discriminator = PeopleClass::getName;
   AtomicInteger index = new AtomicInteger();
   List<List<PeopleClass>> groups = new ArrayList<>(people.stream()
      .sorted(Comparator.comparing(discriminator))
      .collect(Collectors.groupingBy(e -> index.getAndIncrement() % numOfGroups))
      .values());

   //groups.forEach(System.out::println);
   groups.forEach(System.out::println);
   String txt = "";

   for(int j = 0; j < groups.size(); j ++) {
      txt += "Group" + (j + 1);
      txt += "\r\n";
      txt += groups.get(j);
      txt += "\r\n";
      txt += "\r\n";
   }

   return txt;
}

My people class

public PeopleClass(String name){
   this.name = name;
}

But every time I use this, the groups seem to be not random but groups as the order of the original ArrayList of names. How should I fix this and make random.




Aucun commentaire:

Enregistrer un commentaire