Skip to content

Instantly share code, notes, and snippets.

@LutfiTekin
Created March 29, 2018 15:46
Show Gist options
  • Save LutfiTekin/ba543248f9b3505c8c80cb0a0451ac2e to your computer and use it in GitHub Desktop.
Save LutfiTekin/ba543248f9b3505c8c80cb0a0451ac2e to your computer and use it in GitHub Desktop.
Shuffle a string
public static String shuffle(String string) {
StringBuilder sb = new StringBuilder(string.length());
double rnd;
for (char c: string.toCharArray()) {
rnd = Math.random();
if (rnd < 0.34)
sb.append(c);
else if (rnd < 0.67)
sb.insert(sb.length() / 2, c);
else
sb.insert(0, c);
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment