Class WeightRandom<T>

java.lang.Object
com.sure.tool.lang.WeightRandom<T>

public class WeightRandom<T> extends Object
权重随机:按权重概率随机取一个对象,零依赖。

权重须为正数;总权重决定各元素命中概率 = 元素权重 / 总权重。

Since:
0.2.0
Author:
suretool
  • Constructor Details

    • WeightRandom

      public WeightRandom()
      构造(使用默认随机源)。
    • WeightRandom

      public WeightRandom(Random random)
      构造。
      Parameters:
      random - 随机源
  • Method Details

    • add

      public void add(T item, double weight)
      添加权重项。
      Parameters:
      item - 元素
      weight - 权重(>0)
    • clear

      public void clear()
      清空。
    • size

      public int size()
      当前项数量。
      Returns:
      数量
    • next

      public T next()
      按权重随机取一个元素;无元素返回 null。
      Returns:
      元素
    • items

      public List<WeightRandom.WeightObj<T>> items()
      获取全部权重项(副本)。
      Returns:
      权重项列表