我是靠谱客的博主 大意电灯胆,这篇文章主要介绍依照权重随机抽取元素,现在分享给大家,希望可以做个参考。

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

[代码] [Java]代码 import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.TreeBag;
/**
http://www.fpjinan.com/linked/20130223.do; * 依照权重随机抽取元素
* @author heyang
*
*/
public class RandomUtil {
/**
* 依据指定的样本数组和样本-权重映射随机抽取一个元素
* @param elementArray 样本数组
* @param weightMap 样本-权重映射
* @return
* @throws Exception
*/
public static Object randomByWeight(Object[] elementArray,Map weightMap) throws Exception{
if(elementArray==null||elementArray.length==0){
throw new Exception("ElementArray must not be empty!");
}
if(weightMap==null|| weightMap.isEmpty()){
throw new Exception("WeightMap must not be empty!");
}
int[] weightArray = new int[elementArray.length 1];
Object[] tempElementArray = new Object[elementArray.length];
for(int i=0;i desArray[middle]

转载于:https://my.oschina.net/u/947963/blog/110031

最后

以上就是大意电灯胆最近收集整理的关于依照权重随机抽取元素的全部内容,更多相关依照权重随机抽取元素内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部