Files
archie/internal/domain/sampling/random.go
T
sam 01b691142c chore(lint): drop nolint directives that suppress nothing
29 of 59 directives did nothing. Removing each one individually and
re-running the gate revealed no issue from any of them: 20 named gosec,
which is not enabled in .golangci.yml and never has been, plus 4 nilerr
in functions that return no error, 2 dupl where dupl reports nothing
repo-wide, and one each of contextcheck, nestif and unused. The funlen
name on settings.go:147 is gone for the same reason; its gocyclo and
cyclop names are both live.

The unused directive in webui/authorize.go sat on `var _ = slog.Default`,
a statement that only existed to keep the log/slog import alive. Both go.

The 30 remaining directives all suppress a real issue.
2026-10-08 23:29:19 +11:00

33 lines
987 B
Go

package sampling
import (
"context"
"math/rand/v2"
)
// randomSampler is the "random" strategy: a uniform random subset, seeded
// per call from Request.Seed so the same (candidates, req) pair always
// produces the same selection -- a package-level or struct-level generator
// would not give that guarantee under concurrent or repeated calls.
type randomSampler struct{}
// NewRandom builds the "random" strategy.
func NewRandom() Sampler { return randomSampler{} }
func (randomSampler) Name() string { return "random" }
func (randomSampler) Sample(_ context.Context, candidates []Candidate, req Request) ([]Candidate, error) {
if len(candidates) == 0 {
return nil, nil
}
n := effectiveCap(req.Cap, len(candidates))
seed := uint64(req.Seed) // deterministic seeding, not a security boundary
rng := rand.New(rand.NewPCG(seed, seed))
order := rng.Perm(len(candidates))
out := make([]Candidate, n)
for i := range n {
out[i] = candidates[order[i]]
}
return out, nil
}