Created
March 29, 2018 15:46
-
-
Save LutfiTekin/ba543248f9b3505c8c80cb0a0451ac2e to your computer and use it in GitHub Desktop.
Shuffle a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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