Java Collections has a (surprisingly) simple solution to this problem: Collections.shuffle(Collection<?>, Random)
with a Random
seeded with same seed.
List<Integer> quests = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> answers = Arrays.asList(10, 20, 30, 40, 50);
long seed = System.nanoTime();
Collections.shuffle(quests, new Random(seed));
Collections.shuffle(answers, new Random(seed));
System.out.println(quests);
System.out.println(answers);
Note:
Extra optimization is dangerous.
This DOE NOT WORK:
long seed = System.nanoTime();
Random rnd = new Random(seed);
Collections.shuffle(quests, rnd);
Collections.shuffle(answers, rnd);
Originally posted at: https://stackoverflow.com/a/44863528/1506477