package weightedrandom import ( "crypto/rand" "math" "math/big" ) type ordered interface { int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | string } // RandomFunc is the Type for random values required for this type RandomFunc func(int) int // AdjustFunc is the function that returns an adjusted value for the weighting type AdjustFunc func(itemweight int, totalWeight int) int // AccessModifier is executed before an item is returned and returns the modified weight of an item. type AccessModifier func(oldweight int) int // RandInt is the standard function for returning random numbers. It returns an random number between 0 and max func RandInt(max int) int { out, err := rand.Int(rand.Reader, big.NewInt(int64(max))) if err != nil { panic(err) } return int(out.Int64()) } // StandardAdjustFunc returns the normal weight for the adjustment func StandardAdjustFunc(standardweight, _ int) int { return standardweight } // ReverseWeightAdjustFunc returns the value relative to the total weight func ReverseWeightAdjustFunc(weight, totalWeight int) int { return totalWeight / weight } // Addone adds one until the max value is reached, after that it returns only the max value func Addone(oldweight int) int { if oldweight != math.MaxInt { return oldweight + 1 } return oldweight } type weightitem[T any] struct { weight int adjusted int value T }