I'm building a mock roulette wheel selection function for genetic algorithm. First of, I would want to add up the sum
of the fitnessScore
in the main function. After adding up the fitnessScore
I wanted to randomize a value out of that sum
using the math/rand
package in Go. How should I use the rand package in this scenario how do I fix spin_wheel := rand.sum
in order to random a value?
package main
import(
"fmt"
"time"
"math/rand"
)
func rouletteWheel(fitnessScore []float64) []float64{
sum := 0.0
for i := 0; i < len(fitnessScore); i++ {
sum += fitnessScore[i]
}
rand.Seed(time.Now().UnixNano())
spin_wheel := rand.sum
partial_sum := 0.0
for i := 0; i < len(fitnessScore); i++{
partial_sum += fitnessScore[i]
if(partial_sum >= spin_wheel){
return fitnessScore
}
}
return fitnessScore
}
func main(){
fitnessScore := []float64{0.1, 0.2, 0.3, 0.4}
fmt.Println(rouletteWheel(fitnessScore))
}