You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

24 lines
421 B

package rands
import (
"fmt"
"testing"
)
func TestWeightedChoice(t *testing.T) {
choices := []Choice[int, int]{
{Weight: 10, Item: 300},
{Weight: 1, Item: 100},
{Weight: 5, Item: 200},
}
times := make(map[int]int)
for range 100000 {
c, ok := WeightedChoice(choices)
if !ok {
t.Error("weight choice error")
return
}
times[c.Item]++
}
fmt.Println(times) // map[100:6156 200:31280 300:62564]
}