Skip to content

WeightedPool<T>

Namespace: Shadop.Archmage.Sdk

Holds items alongside their selection weights in two parallel arrays of equal length. The Sample and SampleIndex extension methods (in WeightedPoolExtensions) draw an item at random with probability proportional to its weight. The SampleWith and SampleIndexWith methods map a caller-supplied value to an item deterministically according to the weights.

public class WeightedPool<T>

Inheritance ObjectWeightedPool

The candidate values, one per weight.

public T[] Items { get; set; }

T[]

The non-negative selection weights, parallel to WeightedPool.Items.

public Int32[] Weights { get; set; }

Int32[]

The number of items in the pool.

public int Count { get; }

Int32

public WeightedPool()
public WeightedPool(T[] items, Int32[] weights)

items T[]

weights Int32[]

Maps the value to an item deterministically according to the weights. value < 0 returns the first item with non-zero weight; value >= 1 returns the last. Throws if the pool is empty or the total weight is zero.

public T SampleWith(float value)

value Single

T

Maps the value to an item index deterministically according to the weights. value < 0 returns the first item index with non-zero weight; value >= 1 returns the last. Throws if the pool is empty or the total weight is zero.

public int SampleIndexWith(float value)

value Single

Int32