| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package rand
- import (
- "testing"
- )
- func TestSliceWeight(t *testing.T) {
- list := []int32{1, 2, 3}
- weight := NewSliceWeight[int32](list, func(v int32) int32 {
- return v
- })
- for i := 0; i < 10; i++ {
- t.Log(weight.SelectOne())
- }
- }
- func TestMapWeight(t *testing.T) {
- type Item struct {
- Key int32
- Value int32
- }
- maps := map[int32]*Item{
- 1: {
- Key: 1,
- Value: 2,
- },
- 2: {
- Key: 2,
- Value: 3,
- },
- 3: {
- Key: 3,
- Value: 4,
- },
- }
- weight := NewMapWeight[int32](maps, func(v *Item) int32 {
- return v.Value
- })
- for i := 0; i < 10; i++ {
- t.Log(weight.SelectOne())
- }
- }
|