您好,登錄后才能下訂單哦!
//AdvertPushDTO為業務對象,getWeight()獲取權重值
private AdvertPushDTO weightRamdom(List<AdvertPushDTO> list){
int weightSum = 0;
for (AdvertPushDTO dto : list) {
weightSum += dto.getWeight();
}
if (weightSum <= 0) {
System.err.println("錯誤: weightSum=" + weightSum);
return null;
}
Integer n = new Random().nextInt(weightSum); // n的值為 [0, weightSum)
Integer m = 0;
for (AdvertPushDTO dto : list) {
if (m <= n && n < m + dto.getWeight()) {
return dto; //返回結果
}
m += dto.getWeight();
}
return null;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。