T
- 权重随机获取的对象类型public class WeightRandom<T> extends Object implements 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
Modifier and Type | Class and Description |
---|---|
static class |
WeightRandom.WeightObj<T>
带有权重的对象包装
|
Constructor and Description |
---|
WeightRandom()
构造
|
WeightRandom(Iterable<WeightRandom.WeightObj<T>> weightObjs)
构造
|
WeightRandom(WeightRandom.WeightObj<T> weightObj)
构造
|
WeightRandom(WeightRandom.WeightObj<T>[] weightObjs)
构造
|
Modifier and Type | Method and Description |
---|---|
WeightRandom<T> |
add(T obj,
double weight)
增加对象
|
WeightRandom<T> |
add(WeightRandom.WeightObj<T> weightObj)
增加对象权重
|
WeightRandom<T> |
clear()
清空权重表
|
static <T> WeightRandom<T> |
create()
创建权重随机获取器
|
T |
next()
下一个随机对象
|
public WeightRandom()
public WeightRandom(WeightRandom.WeightObj<T> weightObj)
weightObj
- 带有权重的对象public WeightRandom(Iterable<WeightRandom.WeightObj<T>> weightObjs)
weightObjs
- 带有权重的对象public WeightRandom(WeightRandom.WeightObj<T>[] weightObjs)
weightObjs
- 带有权重的对象public static <T> WeightRandom<T> create()
T
- 权重随机获取的对象类型WeightRandom
public WeightRandom<T> add(T obj, double weight)
obj
- 对象weight
- 权重public WeightRandom<T> add(WeightRandom.WeightObj<T> weightObj)
weightObj
- 权重对象public WeightRandom<T> clear()
public T next()
Copyright © 2024. All rights reserved.