Skip to main content

NewWeightedRandom

Given a list of entries with weights, returns WeightedRandomList

def NewWeightedRandom(
ctx: context.Context,
entries: []Entry
) - > WeightedRandomList, error

Given a list of entries with weights, returns WeightedRandomList

Parameters

NameTypeDescription
ctxcontext.ContextThe context for the operation, used for logging and cancellation.
entries[]EntryA slice of Entry objects, each containing an item and its associated weight. Entries with zero weight will be ignored unless all entries have zero weight, in which case they will be assigned equal weights.

Returns

TypeDescription
WeightedRandomList, errorA WeightedRandomList object that can be used to select entries based on their assigned weights, or an error if validation fails.