Skip to content

Instantly share code, notes, and snippets.

@valtyriel
Created June 25, 2012 22:09
Show Gist options
  • Save valtyriel/2991676 to your computer and use it in GitHub Desktop.
Save valtyriel/2991676 to your computer and use it in GitHub Desktop.
roll -- a simple dice-rolling program
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
)
var iflag = flag.Bool("i", false, "print individual rolls")
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
var s string
for _, v := range flag.Args() {
s += v
}
var n, x, c int
_, err := fmt.Sscanf(s, "%dd%d+%d", &n, &x, &c)
if err != nil {
fmt.Fprintln(os.Stderr, "roll:", err)
os.Exit(1)
}
roll := make([]int, n)
for i := range roll {
roll[i] = rand.Intn(x) + 1
}
sum := c
for _, v := range roll {
sum += v
}
if *iflag {
fmt.Println(roll, c)
}
fmt.Println(sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment