方法一
//可能掉哪些宝物
List<DragonPub> dragonPubs = DaoCenter.searchDragonDao.findAllDragon(merLevel);
List<Integer> list = new ArrayList<Integer>();//已经随机到的物品
int length = 0;//需要随机的总个数
int sumRate = 0;//所有宝物的总机率
try {
sumRate = DaoCenter.searchDragonDao.findRateAllDragons(merLevel);
} catch (Exception e) {
}
int rand=CalculateUtil.calRandomValue(sumRate,sumRate*10);
if (sumRate != 0){
int num = rand %sumRate+1;
while (true){
for (int i=0;i<dragonPubs.size();i++){
DragonPub pub = dragonPubs.get(i);
num -= pub.getDragonPub_rate();
if (num<=0){//则随机到了该物品
rand=CalculateUtil.calRandomValue(sumRate,sumRate*10);
num = rand %sumRate+1;
if (list.contains(pub.getGoodPub_id())){//不让随机相同的物品
continue;
}else{
length++;
list.add(pub.getGoodPub_id());//把随机到的物品ID放在list里边避免重复
if (length==dragonRefreshDragonCount){
break;
}
}
}
}
if (length == dragonRefreshDragonCount){
break;
}
}
}
方法二
public static import java.util.Random;
int calRandomValue(int minValue, int maxValue) {
Random random = new Random();
return minValue + random.nextInt(maxValue - minValue + 1);
}
参数为:随机的范围