From c991679f760fc792fc74dafbd4fbcdc826c44c04 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 26 Jan 2017 01:43:11 -0500 Subject: roll: use crypto/rand, D&D is serious business --- roll.go | 17 ++++++++++++----- 1 file 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 */ +/* Copyright (C) 2011, 2013-2014, 2017 Luke Shumaker */ package main import ( + "crypto/rand" "fmt" - "math/rand" + "math/big" "os" "regexp" "strconv" - "time" ) func usage() { fmt.Printf("Arguments are in the format []d[+]\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() } -- cgit v1.2.3-2-g168b