Hi I am trying to shuffle a word completely randomly and this is what I have done (using the fischer-yates method)
private static String scrambleWord(String randomWord) {
/** Scrambles the random word chosen from the text file
*
* @param randomWord A string with the word randomly chosen from the list of words
* @return scrambledWord A string with the word chosen shuffled at random
*/
// Convert string to an array of characters
char[] wordArray = randomWord.toCharArray();
Random random = new Random();
// Start from last element and swap each time
for (int i = wordArray.length-1; i >= 0; i--) {
// Pick a random index
int j = random.nextInt(i+1);
// Swap specific index with random index
char letter = wordArray[i];
wordArray[i] = wordArray[j];
wordArray[j] = letter;
}
String scrambledWord = String.valueOf(wordArray);
return scrambledWord;
}
However, sometimes I find that on the off chance this happens: