github.com/yaricom/goNEAT@v0.0.0-20210507221059-e2110b885482/neat/utils/math_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  	"math/rand"
     6  )
     7  
     8  func TestSingleRouletteThrow(t *testing.T) {
     9  	rand.Seed(42)
    10  	probs := []float64{.1, .2, .4, .15, .15}
    11  
    12  	hist := make([]float64, len(probs))
    13  	runs := 10000
    14  	for i := 0; i < runs; i++ {
    15  		index := SingleRouletteThrow(probs)
    16  		if index < 0 || index >= len(probs) {
    17  			t.Error("invalid segment index", index)
    18  			return
    19  		}
    20  		// increment histogram to check distribution quality
    21  		hist[index] += 1
    22  	}
    23  	t.Log(hist)
    24  }