Skip to content

Instantly share code, notes, and snippets.

@jcmartins
Created March 8, 2016 18:37
Show Gist options
  • Save jcmartins/1af6577ab0dcb4826ebd to your computer and use it in GitHub Desktop.
Save jcmartins/1af6577ab0dcb4826ebd to your computer and use it in GitHub Desktop.
embaralha.go
func main() {
names := []string{"joao", "alex", "ze", "gleicon", "mane", "maria"}
fmt.Printf("names : %v\n", names)
shuffleList := names
shuffle(shuffleList)
fmt.Printf("Shuffled : %v\n", shuffleList)
}
func shuffle(arr []string) {
rand.Seed(int64(time.Now().Nanosecond())) // no shuffling without this line
/*
for i := len(arr) - 1; i > 0; i-- {
j := rand.Intn(i)
arr[i], arr[j] = arr[j], arr[i]
}
*/
for _, i := range rand.Perm(len(arr)) {
joao := arr[i]
fmt.Printf("embaralhado : %v\n", joao)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment