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) // } //}