cdf tweak

This commit is contained in:
2026-03-07 22:52:38 -05:00
parent 1146535093
commit 730b59797a

View File

@@ -411,9 +411,9 @@ func choose(p []*value) int {
// Generation: // Generation:
// 1. Generate a uniformly-random value x in the range [0,1) // 1. Generate a uniformly-random value x in the range [0,1)
// 2. Using a binary search, find the index of the smallest element in cdf larger than x // 2. Using a binary search, find the index of the smallest element in cdf larger than x
var val float64
// multiply the sample with the largest CDF value; easier than normalizing to [0,1) // multiply the sample with the largest CDF value; easier than normalizing to [0,1)
val = rand.Float64() * cdf[len(cdf)-1] val := rand.Float64() * cdf[len(cdf)-1]
// Search returns the smallest index i such that cdf[i] > val // Search returns the smallest index i such that cdf[i] > val
return sort.Search(len(cdf), func(i int) bool { return cdf[i] > val }) return sort.Search(len(cdf), func(i int) bool { return cdf[i] > val })
} }