Java权重随机的实现方法
程序员文章站
2024-03-01 13:29:46
本文实例讲述了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程序设计有所帮助。