Weighter
A type alias for a function that computes the weight of an object for weighted random selection.
The Weighter is a function that takes an element of type E and returns a Double representing its weight. It is used in cases where elements do not implement Weighted directly, allowing custom weight computation.
Example
data class Item(val name: String, val importance: Double)
val items = listOf(
Item("Basic", 1.0),
Item("Advanced", 2.0),
Item("Premium", 5.0)
)
val selector = RandomSelector.fromWeightedIterable(items) { it.importance }
println(selector.pick()) // Outputs an item based on its importance
Content copied to clipboard
Parameters
E
The type of the object for which the weight is being computed.