-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
68 lines (62 loc) · 1.57 KB
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package diceprob
import (
"math/rand"
"sort"
"time"
)
// New - Create a new DiceProb instance.
func New(s string) (*DiceProb, error) {
// Create our object.
obj := &DiceProb{
expression: s,
parsed: &Expression{},
distribution: &map[int64]int64{},
probabilities: &map[int64]float64{},
bounds: &[]int64{},
outcomes: &[]int64{},
permutations: int64(0),
}
// Parse the expression and put it into the object.
var err error
obj.parsed, err = diceParser.ParseString("", obj.expression)
if err != nil {
return nil, err
}
// Return the object.
return obj, nil
}
// rollIt - Using the selected method, roll n dice of s faces, and return the sum.
func rollIt(method string, n int64, s int64) int64 {
// Seed the randomizer.
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Depending on the method...
switch method {
// Mid rolling method.
case "m":
// Initialize the array of rolls.
ret := []int64{}
// Loop three times...
for i := int64(1); i <= 3; i++ {
// Appending a roll to the array.
ret = append(ret, (r.Int63n(s) + 1))
}
// Sort the array numerically.
sort.Slice(ret, func(i, j int) bool { return ret[i] < ret[j] })
// Return the middle value.
return ret[1]
// "Standard" rolling method.
case "d":
// Initialize the return value.
ret := int64(0)
// Loop from 1 to n...
for i := int64(1); i <= n; i++ {
// Add the value of the roll to the return value.
ret = ret + (r.Int63n(s) + 1)
}
// Return the summed roll.
return ret
// Should not reach.
default:
panic("invalid rollIt method")
}
}