简体   繁体   中英

Shuffling of an array

I have written a method to shuffle a String array So the task is to implement WhiteElephant concept( All of the participants' names are placed into a container (hat, box, bag, etc) and mixed up. Each person then chooses one name from the container, but doesn't tell anyone which name was picked. He/she is now responsible for buying a gift for the person selected. ) for a given string array of list of names.Should generate assignments to match the original elements. I have written method to pick a random number and used a map to store the values so that each array value will have a different index. But this prints out only 5 values. and i am confused now. ** A person must be assigned to another person; no person must be assigned to himself/herself.

public static String[] generateAssignments(final String[] participants) {

   Random r = new Random();
   int size = participants.length;
   HashMap val = new HashMap();
   int change = 0;
   String[] assignments = new String[6];
   System.out.println("Using arrays");


   for(int i=0; i<size;i++) {
       for(int j =0; j<size; j++) {
           change = r.nextInt(size);
           if(val.containsValue(change) || change==i) {
               continue;
           }
           else val.put(i, change);
           assignments[i] = participants[change];
           System.out.println(assignments[i]);
           break;
       }   

   }

   return assignments;
}

I appreciate your inputs. Thanks, Lucky

If the point is to just have a mapping of people can't you just create an array that is of the appropriate length and store an int in each one?

ie

int len = participants.length;
int[] map = new int[len];
Random r = new Random();
String list = "";
for(int i = 0; i < len; i++){
   int n = r.nextInt(0, len);
   while((n == i) || (list.contains(n.toString()))){
      n = r.nextInt(0, len);
   }
   list = list + n.toString();
   map[i] = n;
}

That way you've filled an array with ints corresponding to people who are also assigned ints.

I think you could use Collections.shuffle(List)

public static String[] generateAssignments(final String... participants) {
  List<String> al = Arrays.asList(participants.clone());
  out: while (true) {
    Collections.shuffle(al);
    for (int i = 0; i < participants.length; i++) {
      if (al.get(i).equals(participants[i])) {
        continue out;
      }
    }
    break out;
  }
  return al.toArray(new String[0]);
}
public static void main(String[] args) {
  String[] in = new String[] {"Hello", "World", "Goodbye"};
  System.out.println(Arrays.toString(in));
  String[] out = generateAssignments(in);
  System.out.println(Arrays.toString(out));
}

This works for me locally:

/**
 * Assigns secret santa giftees.
 * 
 * @param participants
 *            The participants in the secret santa drawing
 * @return An array of assigned giftees for the participants.
 */
public static String[] generateAssignments(final String[] participants) {
    String[] ret = new String[participants.length];
    Arrays.fill(ret, "Andreas");
    return ret;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM