Skip to content

Instantly share code, notes, and snippets.

@nictuku
Created November 5, 2011 03:17
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 nictuku/1341049 to your computer and use it in GitHub Desktop.
Save nictuku/1341049 to your computer and use it in GitHub Desktop.
go snippet: Using the rand package to randomize access to a slice.
package main
import (
"fmt"
"rand"
"time"
)
func main() {
a := []int64{0, 1, 2, 3, 4, 5, 6}
// Before using the pseudo-number generator we have to initialize it with a seed.
// A simple way is to use the last digits of the current UNIX timestamp in nanoseconds.
rand.Seed(time.Nanoseconds() % 1e9)
for _, i := range rand.Perm(len(a)) {
fmt.Println(a[i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment