Add pickrand.go
authorColin P. Mccabe <colin@cmccabe.xyz>
Wed, 21 Nov 2018 21:56:42 +0000 (13:56 -0800)
committerColin P. Mccabe <colin@cmccabe.xyz>
Wed, 21 Nov 2018 21:56:42 +0000 (13:56 -0800)
pickrand.go [new file with mode: 0644]

diff --git a/pickrand.go b/pickrand.go
new file mode 100644 (file)
index 0000000..a21efda
--- /dev/null
@@ -0,0 +1,33 @@
+package main
+
+import (
+       "fmt"
+       "os"
+       "path/filepath"
+       "math/rand"
+       "time"
+)
+
+func main() {
+       root := "."
+       if len(os.Args) > 1 {
+               root = os.Args[1]
+       }
+       files := make([]string, 0, 32)
+       err := filepath.Walk(root, func(p string, f os.FileInfo, err error) error {
+               if err != nil {
+                       return err
+               }
+               if !f.IsDir() {
+                       files = append(files, p)
+               }
+               return nil
+       })
+       if err != nil {
+               fmt.Fprintf(os.Stderr, "** Error: %s\n", err.Error())
+               os.Exit(1)
+       }
+       rand.Seed(time.Now().UTC().UnixNano())
+       i := rand.Int31n(int32(len(files)))
+       fmt.Printf("%s\n", root + "/" + files[i])
+}