T
- 权重随机获取的对象类型public class WeightRandomSelector<T> extends Object implements Selector<T>, Serializable
平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的。如广告投放、负载均衡等。
如有4个元素A、B、C、D,权重分别为1、2、3、4,随机结果中A:B:C:D的比例要为1:2:3:4。
参考博客:https://www.cnblogs.com/waterystone/p/5708063.html
Constructor and Description |
---|
WeightRandomSelector()
构造
|
WeightRandomSelector(Iterable<WeightObj<T>> weightObjs)
构造
|
WeightRandomSelector(WeightObj<T> weightObj)
构造
|
WeightRandomSelector(WeightObj<T>[] weightObjs)
构造
|
Modifier and Type | Method and Description |
---|---|
WeightRandomSelector<T> |
add(T obj,
int weight)
增加对象
|
WeightRandomSelector<T> |
add(WeightObj<T> weightObj)
增加对象权重
|
WeightRandomSelector<T> |
clear()
清空权重表
|
static <T> WeightRandomSelector<T> |
of()
创建权重随机获取器
|
T |
select()
下一个随机对象
|
public WeightRandomSelector()
public WeightRandomSelector(WeightObj<T> weightObj)
weightObj
- 带有权重的对象public WeightRandomSelector(Iterable<WeightObj<T>> weightObjs)
weightObjs
- 带有权重的对象public static <T> WeightRandomSelector<T> of()
T
- 权重随机获取的对象类型public WeightRandomSelector<T> add(T obj, int weight)
obj
- 对象weight
- 权重public WeightRandomSelector<T> add(WeightObj<T> weightObj)
weightObj
- 权重对象public WeightRandomSelector<T> clear()
Copyright © 2025. All rights reserved.