diff options
-rw-r--r-- | roll.go | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -1,19 +1,27 @@ -/* Copyright (C) 2011, 2013-2014 Luke Shumaker <lukeshu@sbcglobal.net> */ +/* Copyright (C) 2011, 2013-2014, 2017 Luke Shumaker <lukeshu@sbcglobal.net> */ package main import ( + "crypto/rand" "fmt" - "math/rand" + "math/big" "os" "regexp" "strconv" - "time" ) func usage() { fmt.Printf("Arguments are in the format [<COUNT>]d<SIZE>[+<MOD>]\n") } +func rollDie(size int) int { + num, err := rand.Int(rand.Reader, big.NewInt(int64(size))) + if err != nil { + panic(err) + } + return int(num.Int64()) + 1 +} + func roll(input string) { parser := regexp.MustCompile("^([0-9]*)d([0-9]+)([+-][0-9]+)?$") parts := parser.FindStringSubmatch(input) @@ -33,7 +41,7 @@ func roll(input string) { total := 0 for i := 0; i < dice; i++ { - v := rand.Intn(die_size) + 1 + v := rollDie(die_size) fmt.Printf("%d+", v) total += v } @@ -42,7 +50,6 @@ func roll(input string) { } func main() { - rand.Seed(time.Now().UTC().UnixNano()) if len(os.Args) == 1 { usage() } |