A golang package for weighted random.
items := []weightedrand.WeightedItem[string]{
{Weight: 5, Item: "Hi!"},
{Weight: 3, Item: "Hello!"},
{Weight: 2, Item: "What's up?"},
}
wr := weightedrand.New(time.Now().UnixNano(), items)
fmt.Println(wr.GetItem()) // "Hi!", "Hello!" or "What's up?"
Weight | Item | Expected Rate |
---|---|---|
5 | "Hi!" | 50 % (= 5 / (5 + 3 + 2)) |
3 | "Hello!" | 30 % (= 3 / (5 + 3 + 2)) |
2 | "What's up?" | 20 % (= 2 / (5 + 3 + 2)) |