Skip to content

Instantly share code, notes, and snippets.

@marcelom
Created June 7, 2013 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelom/5732441 to your computer and use it in GitHub Desktop.
Save marcelom/5732441 to your computer and use it in GitHub Desktop.
Go Slice Shuffle
package main
import (
"fmt"
"math/rand"
"time"
)
func Shuffle(a []int) {
for i := range a {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
rand.Seed(time.Now().UnixNano())
Shuffle(a)
fmt.Println(a)
}
@dubek
Copy link

dubek commented Jul 10, 2015

Take a look at rand.Perm -- might save the need to call rand.Int inside the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment