The docs say “All permutations occur with approximately equal likelihood.” but I do not know if that includes the (however small) possibility of returning the same ordering. I have a method (see below) where during two test runs the list was returned in the original order, maybe.. Other factors may have been at fault, like an anagram could have been selected and the result was a word. The lexicon is ~300,000 words, so I have changed the method to a more suitable test preventing this from being the case.
The method:
private static char[] nextScrambledWord(int wordLength) {
String word;
do {
word = "ABCDEF"; //TODO Get a word from lexicon
} while(word.length() != wordLength);
ArrayList<Character> temp = new ArrayList<Character>(wordLength);
for(int i = 0; i < wordLength; i++) {
temp.add(word.charAt(i));
}
Collections.shuffle(temp);
char[] result = new char[wordLength];
for(int i = 0; i < wordLength; i++) {
result[i] = temp.get(i);
}
return result;
}
I am also curious about the method itself. If anyone has a suggestion on how this can be improved, please feel free to share. Basically the method will pull a word of wordLength from a set, and return a char array of the shuffled result. Rather than polling a word of a known length, I have considered breaking the lexicon into multiple sets based on word length.
Yes, it is possible that the original order is preserved, but, as you say, that is unlikely with longer lists. Try a list with only two elements to see this happen.