summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2017-01-26 01:43:11 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2017-01-26 01:43:11 -0500
commitc991679f760fc792fc74dafbd4fbcdc826c44c04 (patch)
treec470d02f04f846cb77e5da706028fc08d5b8a0b1
parent70c0690e56f8906296513c10a3cd4c37e63f742d (diff)
roll: use crypto/rand, D&D is serious business
-rw-r--r--roll.go17
1 files changed, 12 insertions, 5 deletions
diff --git a/roll.go b/roll.go
index f5f9b67..414b0d2 100644
--- a/roll.go
+++ b/roll.go
@@ -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()
}