replaced certain functions with common dependencies

This commit is contained in:
grimsace
2026-06-29 09:02:30 -05:00
parent 6d35c9443a
commit 5423fd4217
3 changed files with 17 additions and 1 deletions
+1
View File
@@ -36,5 +36,6 @@ require (
golang.org/x/net v0.35.0 // indirect golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect golang.org/x/text v0.22.0 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
+2
View File
@@ -73,6 +73,8 @@ golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gonum.org/v1/gonum v0.14.0 h1:2NiG67LD1tEH0D7kM+ps2V+fXmsAnpUeec7n8tcr4S0=
gonum.org/v1/gonum v0.14.0/go.mod h1:AoWeoz0becf9QMWtE8iWXNXc27fK4fNeHNf/oMejGfU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+14 -1
View File
@@ -10,6 +10,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"gonum.org/v1/gonum/stat"
) )
// DiceStatistics holds the theoretical statistics for a dice roll. // DiceStatistics holds the theoretical statistics for a dice roll.
@@ -590,6 +592,10 @@ func (s *DiceStatistics) populateDerivedFields() {
s.SortedOutcomes = make([]int, 0, len(s.Results)) s.SortedOutcomes = make([]int, 0, len(s.Results))
s.Percentages = make(map[int]float64, len(s.Results)) s.Percentages = make(map[int]float64, len(s.Results))
// Prepare data for weighted mean calculation using gonum
outcomes := make([]float64, 0, len(s.Results))
weights := make([]float64, 0, len(s.Results))
first := true first := true
maxProbability := 0.0 maxProbability := 0.0
for value, probability := range s.Results { for value, probability := range s.Results {
@@ -610,13 +616,20 @@ func (s *DiceStatistics) populateDerivedFields() {
} }
} }
s.Average += float64(value) * probability // Collect data for weighted mean calculation
outcomes = append(outcomes, float64(value))
weights = append(weights, probability)
// Mode: find outcome with highest probability
if probability > maxProbability || (math.Abs(probability-maxProbability) <= probabilityEpsilon && value < s.MostCommon) { if probability > maxProbability || (math.Abs(probability-maxProbability) <= probabilityEpsilon && value < s.MostCommon) {
maxProbability = probability maxProbability = probability
s.MostCommon = value s.MostCommon = value
} }
} }
// Calculate weighted mean using gonum/stat
s.Average = stat.Mean(outcomes, weights)
sort.Ints(s.SortedOutcomes) sort.Ints(s.SortedOutcomes)
s.MaxPercentage = maxProbability * 100 s.MaxPercentage = maxProbability * 100
} }