Skip to content

Instantly share code, notes, and snippets.

@bradylove
Last active December 20, 2015 22:49
Show Gist options
  • Save bradylove/6207719 to your computer and use it in GitHub Desktop.
Save bradylove/6207719 to your computer and use it in GitHub Desktop.
My solution to http://reddit.com/r/dailyprogrammer challenge #130
// My solution to http://reddit.com/r/dailyprogrammer challenge #130
package main
import (
"fmt"
"os"
"math/rand"
"time"
)
func parseString(str string) (rolls, sides int) {
_, _ = fmt.Sscanf(str, "%dd%d", &rolls, &sides)
return
}
func random(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max) + 1
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: dice 2d20")
return
}
rolls, sides := parseString(os.Args[1])
fmt.Println("Rolls:", rolls, "\nSides:", sides)
for i := 0; i < rolls; i++ {
fmt.Print(random(1, sides), " ")
}
fmt.Print("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment