package table import ( "fmt" "git.tuxpa.in/a/card_id/common/data" "lukechampine.com/frand" ) // a table is a collection of results type Table struct { raw data.Results TotalProbability float64 } func New(r data.Results) *Table { out := &Table{} out.raw = r for _, v := range out.raw { out.TotalProbability = out.TotalProbability + v.Rate } return out } func (t *Table) RandomResult() data.Result { selector := frand.Float64() * t.TotalProbability sofar := 0.0 for _, v := range t.raw { sofar = sofar + v.Rate if sofar >= selector-0.000000001 { return v } } return data.Result{ Name: fmt.Sprintf("please report this bug: %v", selector), Rate: selector, } }