例行检查
功能描述
注意到 channel 表有 weight 字段,代码层面没有利用,只是随机选了可用渠道,希望能开放这个设置渠道权重的特性
应用场景
bqjvbblv1#
目前,随机选择配合分组已经能应付大部分场景了。
ix0qys7i2#
考虑两种实现方式:
7jmck4yq3#
加权随机的一种实现方式:
package main import ( "fmt" "math/rand" "sort" ) type Item struct { Name string Weight int } type WeightedRandom struct { Items []Item TotalWeight int } func NewWeightedRandom(items []Item) *WeightedRandom { totalWeight := 0 for _, item := range items { totalWeight += item.Weight } return &WeightedRandom{ Items: items, TotalWeight: totalWeight, } } func (wr *WeightedRandom) GetRandomItem() Item { randWeight := rand.Intn(wr.TotalWeight) sort.SliceStable(wr.Items, func(i, j int) bool { return wr.Items[i].Weight < wr.Items[j].Weight }) for _, item := range wr.Items { if randWeight < item.Weight { return item } randWeight -= item.Weight } return Item{} } func main() { items := []Item{ {Name: "A", Weight: 4}, {Name: "B", Weight: 3}, {Name: "C", Weight: 2}, {Name: "D", Weight: 1}, } weightedRandom := NewWeightedRandom(items) var count = make(map[string]int) times := 100000 for i := 0; i < times; i++ { item := weightedRandom.GetRandomItem() count[item.Name]++ } for k, v := range count { fmt.Printf("%s: %.2f\n", k, float64(v)/float64(times)) } }
3条答案
按热度按时间bqjvbblv1#
目前,随机选择配合分组已经能应付大部分场景了。
ix0qys7i2#
考虑两种实现方式:
7jmck4yq3#
加权随机的一种实现方式: