Last active
September 21, 2016 15:14
-
-
Save tkshnwesper/b6775bc68d918db9f93b978779bf32ff to your computer and use it in GitHub Desktop.
String Shuffle in Go
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
// StrShuffle shuffles a string that is passed to it | |
func StrShuffle(str string) string { | |
n := 25 + rand.Intn(25) | |
var runes []rune | |
for _, runeval := range str { | |
runes = append(runes, runeval) | |
} | |
var shuffle = func() bool { | |
if rand.Intn(2) == 1 { | |
return true | |
} | |
return false | |
} | |
var repeat = func() { | |
for i := 0; i < len(runes) - 1; i++ { | |
if shuffle() { | |
temp := runes[i] | |
runes[i] = runes[i + 1] | |
runes[i + 1] = temp | |
} | |
} | |
n-- | |
} | |
for n > 0 { | |
repeat() | |
} | |
return string(runes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment