抽獎算法 隨機算法 區間算法

* 抽獎算法
* 基礎模型構建原理:
* 根據傳入數組[20,38,59,883]
* 構建模型[{"max":20,"min":0},{"max":58,"min":20},{"max":117,"min":58},{"max":1000,"min":117}]
* 隨機new Random().nextInt(total) 獲得當前隨機數 878
* 符合模型區間值 則取出該值所在區間數組的值 3

```

package com.yihou.microservices.provider.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.yihou.commons.util.NumberUtil;
import com.yihou.microservices.provider.base.model.entity.GiftPrize;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.util.*;

/**
 * 抽獎算法
 * 基礎模型構建原理:
 * 根據傳入數組[20,38,59,883]
 * 構建模型[{"max":20,"min":0},{"max":58,"min":20},{"max":117,"min":58},{"max":1000,"min":117}]
 * 隨機new Random().nextInt(total) 獲得當前隨機數 878
 * 符合模型區間值 則取出該值所在區間數組的值 3
 * @author HYF
 * @Date 20201126
 */
@Log4j2
public class LuckyGiftUtils {

    /**
     * 記錄區間節點
     */
    @Data
    public class Node {
        private Integer min;
        private Integer max;

        public  Boolean isIn(Integer val){
            Boolean flag = false;
            if (min<=val && val<=max){
                flag = true;
            }
            return flag;
        }
    }

   
    private static List<Node> lotteries = new ArrayList<>();

    private static Integer total =0;

    /**
     * 開啓隨機
     * @return
     */
    public static Integer lotteryRandom(){
        int randNum = new Random().nextInt(total); //生成一個隨機數
        for (int i = 0; i < lotteries.size(); i++) {
            Node node = lotteries.get(i);
            if (node.isIn(randNum)){
                return i;
            }
        }
        return -1;
    }


    /**

    /**
     * 初始化 集合
     * [{"max":20,"min":0},{"max":58,"min":20},{"max":117,"min":58},{"max":1000,"min":117}]
     * @param list
     * @return
     */
    public List<Node> luckyUtils(List<Integer> list) {
        if (list == null || list.size() == 0) {
            log.warn("概率集合不能爲空");
            return null;
        }
        Collections.sort(list);
        Integer cur = 0;
        for (int i = 0; i < list.size(); i++) {
            Node lottery = new Node();
            lottery.setMin(cur);
            Integer current = cur+list.get(i);
            lottery.setMax(current);
            cur = current;
            total +=list.get(i);
            lotteries.add(lottery);
        }
        log.info("當前總權重 = {}",total);
        log.info("Node List = {}", JSONObject.toJSONString(lotteries));
        return lotteries;
    }

    public static void test1(){
        List<Integer> list = new ArrayList<>();
        int a0 = 20;
        int a1 = 38;
        int a2 = 59;
        int a3 = 883;
        list.add(a0);
        list.add(a1);
        list.add(a2);
        list.add(a3);
        log.info("初始化值={}",JSONObject.toJSONString(list));
        LuckyGiftUtils luckyGiftUtils = new LuckyGiftUtils();
        luckyGiftUtils.luckyUtils(list);
        int i0 = 0;
        int i1 = 0;
        int i2 = 0;
        int i3 = 0;
        for (int i = 0; i <1000 ; i++) {
            int cur = LuckyGiftUtils.lotteryRandom();
//            log.info("{}次 結果={}",i,cur);
            if (cur == 0){
                i0+=1;
            }
            if (cur ==1){
                i1+=1;
            }
            if (cur == 2){
                i2+=1;
            }
            if (cur == 3){
                i3+=1;
            }
        }
        log.info("最終結果爲 i0={} , a0={}",i0,a0);
        log.info("最終結果爲 i1={} , a1={}",i1,a1);
        log.info("最終結果爲 i2={} , a2={}",i2,a2);
        log.info("最終結果爲 i3={} , a3={}",i3,a3);
    }

    public static void main(String[] args) {
        test1();

    }
}

```

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