java 傳入抽獎概率 0~100(包含小數)設計獎池,返回抽獎結果

工具類拿去用吧

可以根據實際情況和需求進行修改



import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

public class LuckyMachineUtil {

        private static List<Integer> stringT=new ArrayList<>();
        private static List<Integer> stringC=new ArrayList<>();
        /**
         * 根據傳入的百分比將參數分組(初始化獎池),抽獎
         */
        public static  boolean  luckDraw(String percentage){
            //解析中獎概率
            boolean statDoubel=judgeDoubel(percentage);
            //判斷能否將小數轉換成整型
            boolean statInt=judgeInt(percentage);
            if(!statInt && !statDoubel){
                System.out.println("概率參數有問題,默認不中獎");
                return false;
            }
            if(statInt){
                stringT=new ArrayList<>();
                stringC=new ArrayList<>();
                BitSet bs = new BitSet(100);
                //生成中獎獎池
                for (int count = 0; count < Integer.valueOf(percentage);) {
                    int random = (int) (Math.random() * 100);
                    if (!bs.get(random)) {
                        bs.set(random);
                        stringT.add(random);
                        count++;
                    }
                }
                //生成不中獎獎池
                for (int count = 0; count < 100;count++) {
                    //輸出不重複的隨機數
                    //System.out.print(i + " ");
                    if(!stringT.contains(count)){
                        stringC.add(count);
                    }
                }
                //獲取中獎隨機數判斷是否中獎
                int random = (int) (Math.random() * 100);
                boolean t =stringT.contains(random);
                boolean f =stringC.contains(random);
                System.out.println("中獎獎池"+stringT.toString());
                System.out.println("不中獎獎池"+stringC.toString());
                System.out.println("號碼"+random);
                if (t==true && f==false){
                    return true;
                }else {
                    return false;
                }
            }else if(statDoubel){
                //將概率轉換成小數
                Double jdoube=Double.valueOf(percentage);
                if(jdoube==0){
                    return false;
                }
                //獲取小數位數
                int p= checkIsDoublePointTwo(jdoube);
                    stringT = new ArrayList<>();
                    stringC = new ArrayList<>();
                    BitSet bs = new BitSet(100 * (p * 100));
                    for (int count = 0; count < (p * 100); ) {
                        int random = (int) (Math.random() * 100 * (p * 100));
                        if (!bs.get(random)) {
                            bs.set(random);
                            stringT.add(random);
                            count++;
                        }
                    }

                    for (int count = 0; count < 100 * (p * 100);count++) {
                        //輸出不重複的隨機數
                        //System.out.print(i + " ");
                        if(!stringT.contains(count)) {
                            stringC.add(count);
                        }
                    }
                    //獲取中獎隨機數判斷是否中獎
                    int random = (int) (Math.random() * 100 * (p * 100));
                    boolean t = stringT.contains(random);
                    boolean f = stringC.contains(random);
                System.out.println("中獎獎池"+stringT.toString());
                System.out.println("不中獎獎池"+stringC.toString());
                System.out.println("號碼"+random);
                    if (t == true && f == false) {
                        return true;
                    } else {
                        return false;
                    }
            }

            return false;
        }


    /**
     * 嘗試將doube轉換成數字
     * @param doubelString
     * @return
     */
    public static boolean judgeDoubel(String doubelString){
            try {
                Double d=Double.valueOf(doubelString);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }

    /**
     * 嘗試將doube轉換成數字
     * @param doubelString
     * @return
     */
    public static boolean judgeInt(String doubelString){
        try {
            Integer d=Integer.valueOf(doubelString);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 獲取double的位數
     * @param param
     * @return
     */
    public static int checkIsDoublePointTwo(Double param) {
        if (param == null) {
            return 0;
        }
        BigDecimal bd = new BigDecimal(String.valueOf(param));
        String[] ss = bd.toString().split("\\.");
        if (ss.length <= 1){
            return 0;
        }
        return ss[1].length();
    }


    public static void main(String[] args) {
        System.out.println(luckDraw("0"));
    }
}

 

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