JAVA抽奖工具包

实体类

package com.teenway;

import lombok.Data;

import java.math.BigDecimal;

/**
 * @version 1.0
 * @date 2022/11/16 15:53
 */
@Data
public class Raffle {
    
    

    private String prizeName;

    private BigDecimal probability;
}

工具类

package com.teenway;


import cn.hutool.core.util.RandomUtil;

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


/**
 * @author 
 * @version 1.0
 * @date 2022/11/16 16:14
 */
public class RaffleUtil {
    
    

    public static void main(String[] args) throws Exception {
    
    
        List<Raffle> raffleList = new ArrayList<>();
        Raffle raffle1 = new Raffle();
        Raffle raffle2 = new Raffle();
        Raffle raffle3 = new Raffle();
        Raffle raffle4 = new Raffle();
        Raffle raffle5 = new Raffle();
        raffle1.setPrizeName("奖品1");
        raffle1.setProbability(new BigDecimal("60.00"));
        raffle2.setPrizeName("奖品2");
        raffle2.setProbability(new BigDecimal("19.69"));
        raffle3.setPrizeName("奖品3");
        raffle3.setProbability(new BigDecimal("5.00"));
        raffle4.setPrizeName("奖品4");
        raffle4.setProbability(new BigDecimal("5.00"));
        raffle5.setPrizeName("奖品5");
        raffle5.setProbability(new BigDecimal("10.31"));
        raffleList.add(raffle1);
        raffleList.add(raffle2);
        raffleList.add(raffle3);
        raffleList.add(raffle4);
        raffleList.add(raffle5);

        int count0=0;
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        for (int i = 0; i <100000 ; i++) {
    
    
            int b = getPrize(raffleList);
            if(b==0){
    
    
                count0++;
            }else if(b==1){
    
    
                count1++;
            }else if(b==2){
    
    
                count2++;
            }else if(b==3){
    
    
                count3++;
            }else if(b==4){
    
    
                count4++;
            }
        }
        System.out.println("抽奖100000次,抽中奖品1的次数为"+count0);
        System.out.println("抽奖100000次,抽中奖品2的次数为"+count1);
        System.out.println("抽奖100000次,抽中奖品3的次数为"+count2);
        System.out.println("抽奖100000次,抽中奖品4的次数为"+count3);
        System.out.println("抽奖100000次,抽中奖品5的次数为"+count4);
    }

    public static int getPrize(List<Raffle> raffleList) throws Exception {
    
    
//        int rand = (int)(Math.random()*10000);
        int rand = RandomUtil.randomInt(1,10000);
        System.out.println(rand);
        BigDecimal decimal = new BigDecimal(0);
        for (int i = 0; i <raffleList.size() ; i++) {
    
    
            decimal = decimal.add(raffleList.get(i).getProbability());
        }
//        System.out.println(decimal);
        if(!decimal.equals(new BigDecimal("100.00"))){
    
    
            throw new Exception("奖品概率之和不等于1");
        }
        int range = 0;
        for (int i = 0; i <raffleList.size() ; i++) {
    
    
            int start = range+1;
            int end = raffleList.get(i).getProbability().multiply(new BigDecimal(100)).intValue()+range;
            range += raffleList.get(i).getProbability().multiply(new BigDecimal(100)).intValue();
//            System.out.println(raffleList.get(i).getPrizeName()+": "+start+"~"+end);
            if(start<= rand && rand<=end){
    
    
//                System.out.println("抽中奖品:"+raffleList.get(i).getPrizeName());
                return i;
            }
        }
        return 0;
    }
}

抽奖100000次,抽中奖品1的次数为60098
抽奖100000次,抽中奖品2的次数为19679
抽奖100000次,抽中奖品3的次数为4939
抽奖100000次,抽中奖品4的次数为5027
抽奖100000次,抽中奖品5的次数为10257

两种获取随机数方法的比较

    public static void main(String[] args){
    
    
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (int i = 0; i <100 ; i++) {
    
    
            list1.add(RandomUtil.randomInt(0, 100));
            list2.add((int)(Math.random()*100));
        }
        Map<Integer, Long> collect1 = list1.stream().collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
        Map<Integer, Long> collect2 = list2.stream().collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
        System.out.println(collect1);
        System.out.println(collect2);
    }

输出结果

{0=2, 2=1, 3=2, 4=1, 5=1, 6=1, 7=2, 10=1, 13=2, 14=1, 15=1, 16=2, 17=2, 18=1, 19=2, 21=1, 23=3, 24=1, 26=1, 27=2, 29=1, 30=2, 31=2, 36=2, 37=1, 38=2, 40=2, 42=2, 43=1, 49=1, 54=2, 55=1, 57=1, 58=1, 60=2, 62=1, 64=2, 65=3, 66=2, 67=1, 68=2, 70=1, 74=3, 75=1, 77=4, 78=1, 79=1, 80=3, 81=5, 83=1, 84=1, 85=3, 86=1, 88=1, 89=1, 90=2, 91=2, 92=1, 95=1, 98=2, 99=1}
{0=1, 3=3, 6=1, 7=2, 9=2, 10=1, 11=1, 12=2, 13=2, 14=1, 16=1, 17=1, 18=3, 20=1, 21=1, 22=1, 23=1, 24=2, 26=2, 29=2, 31=3, 32=1, 33=2, 36=3, 37=2, 38=1, 39=2, 40=1, 41=1, 44=1, 46=1, 49=1, 51=1, 54=3, 55=3, 56=2, 58=1, 61=1, 62=1, 63=1, 65=1, 67=3, 68=3, 69=2, 71=1, 72=2, 74=2, 75=2, 78=3, 79=1, 80=1, 81=1, 82=1, 83=2, 84=1, 86=1, 89=1, 92=1, 95=1, 97=1, 98=3, 99=3}

猜你喜欢

转载自blog.csdn.net/AnalogElectronic/article/details/128132474