Refuse an option too close to 0 to tell from it, pick a template's field position by a switch, and drop Weighted's unreachable fallback

This commit is contained in:
2026-10-08 12:13:16 +02:00
committed by Lillem4n
parent c337dc4436
commit 31c2f2651d
2 changed files with 23 additions and 6 deletions
+2 -4
View File
@@ -26,10 +26,8 @@ func (s *State) Float64() float64 { return s.rand.Float64() }
func (s *State) Weighted(cum []float64) int {
total := cum[len(cum)-1]
x := s.Float64() * total
if i := sort.Search(len(cum), func(i int) bool { return cum[i] > x }); i < len(cum) {
return i
}
return sort.Search(len(cum), func(i int) bool { return cum[i] >= total }) // x rounded up to the total
// x stays below total: a product of a float64 under 1 and a positive float64 rounds below it.
return sort.Search(len(cum), func(i int) bool { return cum[i] > x })
}
// Seq advances the counter {seq(key)} reads, or {seq()} for an empty key, and returns it.