Created
November 5, 2011 03:17
-
-
Save nictuku/1341049 to your computer and use it in GitHub Desktop.
go snippet: Using the rand package to randomize access to a slice.
This file contains hidden or 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" | |
"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