标签:
int weightSum = 0;
for (Item item : mItems) {
weightSum += item.getWeight();
}
if (weightSum <= 0) {
return null;
}
Random random = new Random();
int randomNum = random.nextInt(weightSum);
Integer m = 0;
for (Item item : mItems) {
if (m <= randomNum && randomNum < m + item.getWeight()) {
return item;
}
m += item.getWeight();
}
标签:
原文地址:http://www.cnblogs.com/mosthink/p/5289092.html