What's Changed
v3 is a breaking change release that migrates to math/rand/v2 and requires Go 1.22+.
Breaking Changes
- Module path:
github.com/mroth/weightedrand/v3 - Go version: Requires Go 1.22+ (for
math/rand/v2) - Removed
PickSource: Replaced byPickWith(r *rand.Rand), which accepts amath/rand/v2source
New Features
PickWith(r *rand.Rand): Explicit custom rand source support, replacing the deprecatedPickSource.uint64internal backing: Supports larger weight sums (addresses #31)
Performance Improvements
Uses more modern slices.SortFunc and slices.BinarySearch from the Go 1.21+ standard library, replacing previous hand-rolled optimized implementations. Combined with math/rand/v2, this yields:
- NewChooser: 5–33% faster across all sizes
- Pick: 2–15% faster across all sizes
Migration Guide (v2 → v3)
Update your import path:
import "github.com/mroth/weightedrand/v3"If you were using PickSource, switch to PickWith:
// v2
rs := rand.New(rand.NewSource(seed))
item := c.PickSource(rs)
// v3
rs := rand.New(rand.NewPCG(seed1, seed2))
item := c.PickWith(rs)If you only use Pick(), no changes are needed beyond the import path.
For Go < 1.22, use v2: go get github.com/mroth/weightedrand/v2
Full Changelog: v2.1.0...v3.0.0