As I understand it, you're asking whether this Java program is guaranteed to print bcdea
on every Java implementation in existence.
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
class Main {
public static void main(String[] args) {
Random rng = new Random(42);
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
Collections.shuffle(list, rng);
for (String s : list) System.out.print(s);
}
};
tio.run says that it produces the same output at least between "OpenJDK 8" and whatever-it-is they bill as "JDK". But that's an extremely small and boring sample.
The official Oracle docs are not reassuring:
Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair.
This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive. [Emphasis added.]
That is, Oracle doesn't claim that every implementation works that way; nor do they even bother to document in what way the swapped element is "randomly selected."
Btw, I arrived at your question after seeing Collections.shuffle
used in U.S. voting system software with exactly this assumption: that its behavior is reproducible no matter what JDK you're using. I agree that it's not at all clear whether this is the case.
(It's not the case for C++ std::shuffle
, by the way. There, different library implementations can and do give different shuffles for the same input.)