From: Colin P. Mccabe Date: Wed, 21 Nov 2018 21:56:42 +0000 (-0800) Subject: Add pickrand.go X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbb7220527c04fb8b07aa777c2f46c26e35f56ed;p=cmccabe-bin Add 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]) +}