Skip to content

Instantly share code, notes, and snippets.

@178inaba
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save 178inaba/eb947bf8e9763f05d22a to your computer and use it in GitHub Desktop.

Select an option

Save 178inaba/eb947bf8e9763f05d22a to your computer and use it in GitHub Desktop.
generate random slice key from go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
box := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
fmt.Println("len:", len(box))
fmt.Println("Unix:", time.Now().Unix())
fmt.Println("UnixNano:", time.Now().UnixNano())
rand.Seed(time.Now().UnixNano())
for i := 0; i < 50; i++ {
k := rand.Intn(len(box))
fmt.Println("key:", k)
fmt.Println("box:", box[k])
}
}
len: 10
Unix: 1257894000
UnixNano: 1257894000000000000
key: 0
box: a
key: 8
box: i
key: 7
box: h
key: 2
box: c
key: 3
box: d
key: 9
box: j
key: 4
box: e
key: 7
box: h
key: 8
box: i
key: 4
box: e
key: 2
box: c
key: 5
box: f
key: 9
box: j
key: 5
box: f
key: 2
box: c
key: 2
box: c
key: 4
box: e
key: 8
box: i
key: 8
box: i
key: 8
box: i
key: 6
box: g
key: 8
box: i
key: 0
box: a
key: 8
box: i
key: 4
box: e
key: 6
box: g
key: 9
box: j
key: 4
box: e
key: 0
box: a
key: 7
box: h
key: 9
box: j
key: 9
box: j
key: 1
box: b
key: 2
box: c
key: 7
box: h
key: 7
box: h
key: 4
box: e
key: 7
box: h
key: 7
box: h
key: 9
box: j
key: 0
box: a
key: 0
box: a
key: 3
box: d
key: 3
box: d
key: 7
box: h
key: 9
box: j
key: 5
box: f
key: 5
box: f
key: 9
box: j
key: 2
box: c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment