From 730b59797accb0aa21102b94b09868b5c7374ded Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sat, 7 Mar 2026 22:52:38 -0500 Subject: [PATCH] cdf tweak --- microgopt.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/microgopt.go b/microgopt.go index ed4198b..5431c4c 100644 --- a/microgopt.go +++ b/microgopt.go @@ -411,9 +411,9 @@ func choose(p []*value) int { // Generation: // 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 - var val float64 + // 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 return sort.Search(len(cdf), func(i int) bool { return cdf[i] > val }) }