123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package weightrand
- import (
- "fmt"
- "math/rand"
- "testing"
- "time"
- )
- func TestWeightRand(t *testing.T) {
- rand.Seed(time.Now().Unix())
- wr := NewRandTable[string]()
- wr.AddItem("C", 1)
- wr.AddItem("A", 10)
- wr.AddItem("B", 30)
- wr.AddItem("D", 59)
- wr.AddItem("E", 0)
- results := map[string]int{
- "A": 0,
- "B": 0,
- "C": 0,
- "D": 0,
- "E": 0,
- }
- iterations := 10000
- for i := 0; i < iterations; i++ {
- id, ok := wr.GetRandomItem()
- if !ok {
- break
- }
- results[id]++
- }
- // Print the results
- fmt.Printf("Results after %d iterations:\n", iterations)
- for item, count := range results {
- fmt.Printf("%s: %f%%\n", item, float64(count)/float64(iterations)*100)
- }
- }
- func TestWeightRand2(t *testing.T) {
- rand.Seed(time.Now().Unix())
- wr := NewRandTable[string]()
- wr.AddItem("C", 1)
- wr.AddItem("A", 10)
- wr.AddItem("B", 30)
- wr.AddItem("D", 59)
- wr.AddItem("E", 0)
- results := map[string]int{
- "A": 0,
- "B": 0,
- "C": 0,
- "D": 0,
- "E": 0,
- }
- iterations := 10
- count := 0
- for i := 0; i < iterations; i++ {
- id, ok := wr.RandomRemoveItem()
- if !ok {
- break
- }
- results[id]++
- count++
- }
- fmt.Printf("Rand count %d \n", count)
- // Print the results
- fmt.Printf("Results after %d iterations:\n", iterations)
- for item, count := range results {
- fmt.Printf("%s: %f%%\n", item, float64(count)/float64(iterations)*100)
- }
- }
- //
- //func TestSimpleWeightRand(t *testing.T) {
- // rand.Seed(time.Now().Unix())
- // wr := NewSimpleRandTable(4)
- //
- // wr.AddItem(0)
- // wr.AddItem(10)
- // wr.AddItem(30)
- // wr.AddItem(60)
- //
- // results := map[int]int{
- // 0: 0,
- // 1: 0,
- // 2: 0,
- // 3: 0,
- // }
- //
- // iterations := 10000
- //
- // for i := 0; i < iterations; i++ {
- // item := wr.GetRandomItem()
- // results[item]++
- // }
- //
- // // Print the results
- // fmt.Printf("Results after %d iterations:\n", iterations)
- // for item, count := range results {
- // fmt.Printf("%d: %f%%\n", item, float64(count)/float64(iterations)*100)
- // }
- //}
|