diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2017-01-26 01:43:11 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2017-01-26 01:43:11 -0500 |
commit | c991679f760fc792fc74dafbd4fbcdc826c44c04 (patch) | |
tree | c470d02f04f846cb77e5da706028fc08d5b8a0b1 | |
parent | 70c0690e56f8906296513c10a3cd4c37e63f742d (diff) |
roll: use crypto/rand, D&D is serious business
-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() } |