From 5cb5e3bf9bab9e3d27d26e33a2a322f3370e9c37 Mon Sep 17 00:00:00 2001 From: Gator Date: Mon, 2 May 2022 20:29:22 +0200 Subject: [PATCH] seed can be specified to reproduce output --- config.go | 7 +++++++ fs.go | 2 -- main.go | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 0501a2c..b99f2f7 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,7 @@ import ( "os" "strconv" "strings" + "time" ) type Config struct { @@ -21,6 +22,7 @@ type Config struct { Sepia bool Amount int Quiet bool + Seed int64 } // loads configuration values from json file @@ -47,6 +49,9 @@ func (c *Config) SetOutputDimensions() { } func initConfigRegister() { + // default seed for the RNG + seed := time.Now().UnixNano() + // command line arguments flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, average, fuckyfun") flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory") @@ -56,7 +61,9 @@ func initConfigRegister() { flag.BoolVar(&ConfigRegister.Grayscale, "grayscale", false, "Output image in shade of gray (around 50)") flag.BoolVar(&ConfigRegister.Sepia, "sepia", false, "Output image with sepia tone") flag.IntVar(&ConfigRegister.Amount, "amount", 2, "Number of images to blend together") + flag.Int64Var(&ConfigRegister.Seed, "seed", seed, "Number of images to blend together") flag.BoolVar(&ConfigRegister.Quiet, "quiet", false, "No progress messages") + flag.Parse() // set output's width and height diff --git a/fs.go b/fs.go index 0908096..5913737 100644 --- a/fs.go +++ b/fs.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "strings" - "time" ) // load an image from a file into an Image value @@ -52,7 +51,6 @@ func getImagesList(path string) []string { // Randomly choose x number of image from a given folder func getRandomImage() string { - rand.Seed(time.Now().UnixNano()) dir := getImagesList(ConfigRegister.InputDir) index := rand.Intn(len(dir)) return dir[index] diff --git a/main.go b/main.go index 3dd7df6..01aa871 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( _ "image/png" "io/ioutil" "log" + "math/rand" ) func main() { @@ -16,6 +17,10 @@ func main() { } + // init RNG + rand.Seed(ConfigRegister.Seed) + log.Println("Seed :", ConfigRegister.Seed) + // pool of workers unionizing, ready to blend a new picture using the power of friendship var pool blendWorkerPool