您的位置:首页 > 编程语言 > Java开发

Java 模板权重随机

2015-09-09 09:55 471 查看
Template templates=...// 所有的模板
final  int _weights=1000; // 所有的模板权重

Template _template=null;
//随机一个权重
int rand = RandomUtil.nextInt(0, _weights);
int lastEd = 0;
int curEd = 0;
// 根据随机的权重找到对应的模板
for(Template _templ : templates) {
int eden = _templ .getWeight();
curEd += eden;
if(lastEd <= rand && rand < curEd){
_template=_templ;
break;
}
lastEd += eden;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: