package card import ( "fmt" "git.tuxpa.in/a/card_id/common/data" "lukechampine.com/frand" ) // a table is a collection of results type Deck struct { Cards []Card TotalProbability float64 BonusLifeProbability float64 } func NewDeck(r data.Results) *Deck { out := &Deck{} out.BonusLifeProbability = 0.005 out.Cards = make([]Card, 0, len(r)) for _, v := range r { out.Cards = append(out.Cards, new(Card).FromResult(v)) out.TotalProbability = out.TotalProbability + v.Rate } return out } func (t *Deck) Draw() Card { selector := frand.Float64() * t.TotalProbability sofar := 0.0 for _, v := range t.Cards { sofar = sofar + v.Rate if sofar >= selector-0.000000001 { return v.CopyWithBonus(t.BonusLifeProbability) } } return Card{ Id: 0, Name: fmt.Sprintf("please report this bug: %v", selector), Rate: selector, } }