Created
October 20, 2014 14:54
-
-
Save g-dormoy/fef174d87e0f52b8eb36 to your computer and use it in GitHub Desktop.
shuffle slice / demo http://play.golang.org/p/fCaOHAwMer
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
package main | |
import "fmt" | |
import "math/rand" | |
func shuffleSlice(slice []interface{}) []interface{} { | |
for i := range slice { | |
j := rand.Intn(i + 1) | |
slice[i], slice[j] = slice[j], slice[i] | |
} | |
return slice | |
} | |
func main() { | |
list := make([]interface{}, 4) | |
list[0] = "foo" | |
list[1] = "bar" | |
list[2] = "foo1" | |
list[3] = "bar1" | |
list = shuffleSlice(list) | |
fmt.Println(list...) | |
list = shuffleSlice(list) | |
fmt.Println(list...) | |
list = shuffleSlice(list) | |
fmt.Println(list...) | |
list = shuffleSlice(list) | |
fmt.Println(list...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment