Chinaunix首页 | 论坛 | 博客
  • 博客访问: 183325
  • 博文数量: 92
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1413
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-04 21:12
文章分类
文章存档

2013年(92)

我的朋友

分类: 信息化

2013-02-25 05:30:59

[代码] [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;

/**
  ; * 依照权重随机抽取元素
 * @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                                   
阅读(501) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~