diff --git a/dice/main.go b/dice/main.go index 2459cab..2c65c41 100644 --- a/dice/main.go +++ b/dice/main.go @@ -24,6 +24,7 @@ var ( Sides int64 `flag:"sides,s" default:"6" description:"How many sides does the dice have?"` Template string `flag:"template" vardefault:"tpl" description:"Template to format the result with"` Type string `flag:"type,t" default:"dice" description:"What to throw (coin, dice)"` + Unique bool `flag:"unique,u" default:"false" description:"Cannot roll the same number twice"` VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` }{} @@ -115,14 +116,28 @@ func main() { } } +func containsNum(s []int64, n int64) bool { + for _, sn := range s { + if sn == n { + return true + } + } + return false +} + func getRandomNumber(sides int64) int64 { n, _ := rand.Int(rand.Reader, big.NewInt(sides)) return n.Int64() } func getResults(sides, count int64) (res []int64, sum int64) { - for i := int64(0); i < count; i++ { + for len(res) < int(count) { v := getRandomNumber(sides) + 1 + + if cfg.Unique && containsNum(res, v) { + continue + } + res = append(res, v) sum += v }