隨機獲取禮物活動總結(抽獎算法)

/**
 * 獎勵金(禮物)模型
 * Created by tong.hua on 2017-10-26.
 */
public class BountyItem extends ToString{
    private static final long serialVersionUID = -4046365922597449030L;

    //禮物編號
    private int giftId;
    //禮物類型 cash 現金  deduction 抵扣紅包 coupon 加息券
    private String giftType;
    //禮物編號
    private String poolNo;
    //禮物名稱
    private String name;
    //中獎機率
    private String probability;

    public int getGiftId() {
        return giftId;
    }

    public void setGiftId(int giftId) {
        this.giftId = giftId;
    }

    public String getGiftType() {
        return giftType;
    }

    public void setGiftType(String giftType) {
        this.giftType = giftType;
    }

    public String getPoolNo() {
        return poolNo;
    }

    public void setPoolNo(String poolNo) {
        this.poolNo = poolNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProbability() {
        return probability;
    }

    public void setProbability(String probability) {
        this.probability = probability;
    }
}


核心算法

/**
     * 根據概率獲取禮物集合中的禮物
     * @param bountyItems 禮物列表
     * @return
     * 1.把禮物id,禮物的概率放大10000倍,存入map集合中 <giftid,[currentScope,lastScope]>
     *     <1,[1-500]>  <2,[500-1500]>  <3,[1500-3000]>
     * 2.獲取1-10000之間的一個隨機數(幸運數)luckyNumber  比如:1700
     * 3.遍歷map,找出1700所在的區間,獲取幸運id,luckyItemId 3
     * 4.根據luckyItemId,取出bountyItems中對應的禮物
     *
     */
    private BountyItem getBountyItem(List<BountyItem> bountyItems) {
        // 放大倍數
        int mulriple = 10000;
        int lastScope = 0;
        // 洗牌,打亂獎品次序
        Collections.shuffle(bountyItems);
        Map<Integer, int[]> itemScopes = new HashMap<Integer, int[]>();
        for (BountyItem item : bountyItems) {
            int giftId = item.getGiftId();
            // 劃分區間
            int currentScope = lastScope + new BigDecimal(item.getProbability()).multiply(new BigDecimal(mulriple)).intValue();
            itemScopes.put(giftId, new int[] { lastScope + 1, currentScope });
            lastScope = currentScope;
        }

        // 獲取1-10000之間的一個隨機數
        int luckyNumber = new Random().nextInt(mulriple);
        int luckyItemId = 0;
        // 查找隨機數所在的區間
        if (!itemScopes.isEmpty()) {
            Set<Map.Entry<Integer, int[]>> entrySets = itemScopes.entrySet();
            for (Map.Entry<Integer, int[]> m : entrySets) {
                int key = m.getKey();
                if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1]) {
                    luckyItemId = key;
                    break;
                }
            }
        }
        //根據luckyItemId從禮物集合中選中禮物
        BountyItem selectedItem = new BountyItem();
        for(BountyItem item : bountyItems){
            int giftId = item.getGiftId();
            if(giftId == luckyItemId){
                selectedItem = item;
            }
        }
        return selectedItem;
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章