From fbb7220527c04fb8b07aa777c2f46c26e35f56ed Mon Sep 17 00:00:00 2001 From: Colin P. Mccabe Date: Wed, 21 Nov 2018 13:56:42 -0800 Subject: [PATCH] Add pickrand.go --- pickrand.go | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-) create mode 100644 pickrand.go diff --git a/pickrand.go b/pickrand.go new file mode 100644 index 0000000..a21efda --- /dev/null +++ b/pickrand.go @@ -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]) +} -- 1.6.6.rc1.39.g9a42