Java权重随机的实现方法
投稿:shichen2014
这篇文章主要介绍了Java权重随机的实现方法,实例分析了权重随机算法的原理与完整实现方法,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Java权重随机的实现方法。分享给大家供大家参考。具体分析如下:
权重随机在项目中经常用到,所以我把它抽象到一个工具类中。
一般实现随机权重有两种方式:
1. 使用一个数组存放权重对应的实际目标,比如A的权重是2,B的权重是3,那么数组长度为5, 数组前两个存放A,后三个存放B。
然后随机一个[0-数据长度)的数字,直接取数组对应下标的值就可以了。
优点:数据结构简单,算法高效,实现简单
缺点:当权重值比较大同时数据又比较多的时候,会浪费内存
2. 使用区间算法,从前到后依次叠加权重,然后随机一个[1-权重和]的数字,再用随机的权重依次减去每个元素的权重,当第一个小于等于0的元素就是我们找元素
这里实现可以借用Arrays的binarySearch方法。
完整实例代码点击此处本站下载。
贴一下代码:
WeightMeta.java:
复制代码 代码如下:
/**
* 建议使用RandomUtil类创建RandomMeta对象
* @author wxf on 14-5-5.
*/
public class WeightMeta<T> {
private final Random ran = new Random();
private final T[] nodes;
private final int[] weights;
private final int maxW;
public WeightMeta(T[] nodes, int[] weights) {
this.nodes = nodes;
this.weights = weights;
this.maxW = weights[weights.length - 1];
}
/**
* 该方法返回权重随机对象
* @return
*/
public T random() {
int index = Arrays.binarySearch(weights, ran.nextInt(maxW) + 1);
if (index < 0) {
index = -1 - index;
}
return nodes[index];
}
public T random(int ranInt) {
if (ranInt > maxW) {
ranInt = maxW;
} else if(ranInt < 0){
ranInt = 1;
} else {
ranInt ++;
}
int index = Arrays.binarySearch(weights, ranInt);
if (index < 0) {
index = -1 - index;
}
return nodes[index];
}
@Override
public String toString() {
StringBuilder l1 = new StringBuilder();
StringBuilder l2 = new StringBuilder("[random]\t");
StringBuilder l3 = new StringBuilder("[node]\t\t");
l1.append(this.getClass().getName()).append(":").append(this.hashCode()).append(":\n").append("[index]\t\t");
for (int i = 0; i < weights.length; i++) {
l1.append(i).append("\t");
l2.append(weights[i]).append("\t");
l3.append(nodes[i]).append("\t");
}
l1.append("\n");
l2.append("\n");
l3.append("\n");
return l1.append(l2).append(l3).toString();
}
}
* 建议使用RandomUtil类创建RandomMeta对象
* @author wxf on 14-5-5.
*/
public class WeightMeta<T> {
private final Random ran = new Random();
private final T[] nodes;
private final int[] weights;
private final int maxW;
public WeightMeta(T[] nodes, int[] weights) {
this.nodes = nodes;
this.weights = weights;
this.maxW = weights[weights.length - 1];
}
/**
* 该方法返回权重随机对象
* @return
*/
public T random() {
int index = Arrays.binarySearch(weights, ran.nextInt(maxW) + 1);
if (index < 0) {
index = -1 - index;
}
return nodes[index];
}
public T random(int ranInt) {
if (ranInt > maxW) {
ranInt = maxW;
} else if(ranInt < 0){
ranInt = 1;
} else {
ranInt ++;
}
int index = Arrays.binarySearch(weights, ranInt);
if (index < 0) {
index = -1 - index;
}
return nodes[index];
}
@Override
public String toString() {
StringBuilder l1 = new StringBuilder();
StringBuilder l2 = new StringBuilder("[random]\t");
StringBuilder l3 = new StringBuilder("[node]\t\t");
l1.append(this.getClass().getName()).append(":").append(this.hashCode()).append(":\n").append("[index]\t\t");
for (int i = 0; i < weights.length; i++) {
l1.append(i).append("\t");
l2.append(weights[i]).append("\t");
l3.append(nodes[i]).append("\t");
}
l1.append("\n");
l2.append("\n");
l3.append("\n");
return l1.append(l2).append(l3).toString();
}
}
RandomUtil.java:
复制代码 代码如下:
/**
* 随机工具类
*
* 使用权重的集合Map构建随机元数据对象
*
* 比如:
* 我们有3个url地址,他们的权重分别为1,2,3现在我们利用RandomUtil来根据权重随机获取url:
*
* <p><blockquote><pre>
*
* map.put(url1, 1);
* map.put(url2, 2);
* map.put(url3, 3);
* RandomMeta<String, Integer> md = RandomUtil.buildWeightMeta(map);
* String weightRandomUrl = md.random();
*
* </pre></blockquote><p>
*
* @author wxf on 14-5-5.
*/
public class RandomUtil {
public static <T> WeightMeta<T> buildWeightMeta(final Map<T, Integer> weightMap) {
final int size = weightMap.size();
Object[] nodes = new Object[size];
int[] weights = new int[size];
int index = 0;
int weightAdder = 0;
for (Map.Entry<T, Integer> each : weightMap.entrySet()) {
nodes[index] = each.getKey();
weights[index++] = (weightAdder = weightAdder + each.getValue());
}
return new WeightMeta<T>((T[]) nodes, weights);
}
}
* 随机工具类
*
* 使用权重的集合Map构建随机元数据对象
*
* 比如:
* 我们有3个url地址,他们的权重分别为1,2,3现在我们利用RandomUtil来根据权重随机获取url:
*
* <p><blockquote><pre>
*
* map.put(url1, 1);
* map.put(url2, 2);
* map.put(url3, 3);
* RandomMeta<String, Integer> md = RandomUtil.buildWeightMeta(map);
* String weightRandomUrl = md.random();
*
* </pre></blockquote><p>
*
* @author wxf on 14-5-5.
*/
public class RandomUtil {
public static <T> WeightMeta<T> buildWeightMeta(final Map<T, Integer> weightMap) {
final int size = weightMap.size();
Object[] nodes = new Object[size];
int[] weights = new int[size];
int index = 0;
int weightAdder = 0;
for (Map.Entry<T, Integer> each : weightMap.entrySet()) {
nodes[index] = each.getKey();
weights[index++] = (weightAdder = weightAdder + each.getValue());
}
return new WeightMeta<T>((T[]) nodes, weights);
}
}
希望本文所述对大家的Java程序设计有所帮助。