You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 622B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sort"
  6. "time"
  7. )
  8. func roll(x, y int) []int {
  9. rolls := make([]int, x)
  10. for i := 0; i < x; i++ {
  11. rolls[i] = (rand.Intn(y) + 1)
  12. }
  13. return rolls
  14. }
  15. func sum(ary []int) int {
  16. s := 0
  17. for _, v := range ary {
  18. s += v
  19. }
  20. return s
  21. }
  22. func stats() []int {
  23. rolls := make([]int, 6)
  24. for i := 0; i < 6; i++ {
  25. thisRoll := roll(4, 6)
  26. sort.Ints(thisRoll)
  27. top3 := make([]int, 3)
  28. copy(top3, thisRoll[1:])
  29. rolls[i] = sum(top3)
  30. }
  31. return rolls
  32. }
  33. func main() {
  34. rand.Seed(time.Now().UnixNano())
  35. fmt.Println("=== Generated D&D character ===")
  36. fmt.Println("Rolls: ", stats())
  37. }