'How can I randomly pull out a String from an array list and put it in another?
As seen in the code that I tried to write, I want to grab a name from the Array list "names" and either add it to groupA or B. I do not need the groups to be even.
import java.io.File;
import java.io.FileNotFoundException; 
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
public class GroupPicker{
    public static void main(String[] args) throws FileNotFoundException{
        Scanner input = new Scanner(new File("GroupPicker1.txt"));
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> groupA = new ArrayList<String>();
        ArrayList<String> groupB = new ArrayList<String>();
        Random random = new Random();
        while(input.hasNext()){
            String nextName = input.next();
            names.add(nextName);
        }
        for (int i = 0; i < names.size(); i++) {
          names.get(random.nextInt(groupA.add()));
          
          names.get(random.nextInt(groupB.add()));
        }
        System.out.println( "\n" + "project groups: ");
        System.out.println("Group A: " + groupA);
        System.out.println("Group B: " + groupB);   
    }
}
Solution 1:[1]
First you have to make sure the random.nextInt() doesn't give you a too high value, because you only put so much values into names, but random.nextInt() will return any int - this could be even negative!
So the easiest way would be this:
- shuffle the nameslist withCollections.shuffle(names)
- put the first half of namesintogroupA- with the use ofList.subListyou would not even have to create a new list, just use the returned result.
- put the second half of namesintogroupB
You have to take care about the indexes for the "halfes" - and decide what to do if the number of entries in names is odd - put one more into groupA or groupB.
Solution 2:[2]
How can I randomly pull out a String from an array list and put it in another
List<String> names = new ArrayList<>();
Random r = new Random();
- nextInt(n)- returns a value between- 0 and n-1inclusive
- r.nextInt(names.size())- will get one random index of all possible indices
String name = names.get(r.nextInt(names.size());
List<String> otherList = new ArrayList<>();
otherList.add(name);
Note you can also shuffle a list if required.
Collections.shuffle(names);
Then you can simply iterate across the list, getting names in the new random order.
Solution 3:[3]
This is what I would do if I understand your need :
// Scanner input = new Scanner(new File("GroupPicker1.txt"));
      String arr[] = new String[] { "A", "B", "C", "D" };
      ArrayList<String> names = new ArrayList(Arrays.asList(arr));
      ArrayList<String> groupA = new ArrayList<String>();
      ArrayList<String> groupB = new ArrayList<String>();
      
      // while(input.hasNext()){
      //     String nextName = input.next();
      //     names.add(nextName);
      // }
      while (!names.isEmpty()) {
        for (int i = 0; i < names.size(); i++) {
          int random = ThreadLocalRandom.current().nextInt(i, names.size());
          boolean randBool = Math.random() < 0.5;
          String name = names.get(random);
          names.remove(random);
          if (randBool){ 
            groupA.add(name);
            break;
          };
          groupB.add(name);
        }  
      }
      
      System.out.println( "\n" + "project groups: ");
      System.out.println("Group A: " + groupA);
      System.out.println("Group B: " + groupB); 
Solution 4:[4]
When I read this problem, two conditions stand out.
- the names must go into one of the lists (meaning not the other) -- you must take it from the names ArrayList
- the names do not need to be even -- you do not need to keep track of which list they are being added to
I would use an enhanced for-loop for ease of use (the less variables the better)
    // this cycles through each index of the names ArrayList
    for(String singleName : names)
    {
        // creates a random number
        java.util.Random number = new java.util.Random();
        // add to groupA if it is an even number and groupB if it is odd
        if(number.nextInt(2) % 2 == 0)
            groupA.add(singleName);
        else
            groupB.add(singleName);
    }
EDIT AFTER SHOWING MY PROFF XD
For simplicities sake, you could even cut off the % 2 == 0 because 0 and 1 will only ever give you 0 or 1 with that statement. This could look like this:
    java.util.Random rand = new java.util.Random();
    // true = one list && false = the other
    if(rand.nextBoolean())
        groupA.add(singleName);
    else
        groupB.add(singleName);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | cyberbrain | 
| Solution 2 | |
| Solution 3 | Zabon | 
| Solution 4 | 
