Skip to content

Instantly share code, notes, and snippets.

@yiting007
Created May 4, 2015 22:55
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 yiting007/043701b6c36fa97d0192 to your computer and use it in GitHub Desktop.
Save yiting007/043701b6c36fa97d0192 to your computer and use it in GitHub Desktop.
Shuffle an array in Golang
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
array := []int{1, 2, 3, 4, 5, 6, 7}
random(array)
}
func random(array []int) {
rand.Seed(time.Now().UTC().UnixNano())
for i := len(array) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
array[i], array[j] = array[j], array[i]
}
fmt.Println(array)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment