从业十余年

导航

大转盘 算法介绍

1.创建奖品的奖品实体类

package test01;

//奖品类
public class Prize {
    private String prizeName;//名称
    private String prizeCost;//价格
    
    private int prizeNum;//权重
    private int prizeSum;//设置奖品总数
    
    public Prize(String prizeName, String prizeCost, int prizeNum,int prizeSum) {
        this.prizeName = prizeName;
        this.prizeCost = prizeCost;
        this.prizeNum = prizeNum;
        this.prizeSum=prizeSum;
    }
    
    public int getPrizeSum() {
        return prizeSum;
    }

    public void setPrizeSum(int prizeSum) {
        this.prizeSum = prizeSum;
    }

    public int getPrizeNum() {
        return prizeNum;
    }
    public void setPrizeNum(int prizeNum) {
        this.prizeNum = prizeNum;
    }
    public String getPrizeName() {
        return prizeName;
    }
    public void setPrizeName(String prizeName) {
        this.prizeName = prizeName;
    }
    public String getPrizeCost() {
        return prizeCost;
    }
    public void setPrizeCost(String prizeCost) {
        this.prizeCost = prizeCost;
    }
}

2.创建微信大转盘的算法 概率控制的代码

package test01;

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

//计算类
public class GashaponMachines {
    private List<Prize> prizes;
    // 概率
    private static BigDecimal LOTTERY_PROBABILITY_DERIVATIVE = new BigDecimal(0);
    // 商品权重
    private long num;
    // 设置随机数的范围
    private BigDecimal odds;
    
    
    //返回对象 
    private Message message;

    public GashaponMachines() {
        prizes = new ArrayList<Prize>();
        num = 0;
    }

    public void addPrizes(Prize prize) {
        prizes.add(prize);
        // 商品的总数 0-500 501-1000 1001-1500
        num += prize.getPrizeNum();

    }

    // 获取随机数的范围
    public BigDecimal getOdds() {
        // 随机数范围
        odds = new BigDecimal(num).multiply(LOTTERY_PROBABILITY_DERIVATIVE); // 设置随机数的范围
        System.out.println("随机数的范围为" + odds.longValue());
        return odds;
    }

    // 获取概率范围
    public BigDecimal getlotteryProbabilityDerivative(String lotteryProbability,int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("这个精度必须大于零或者等于零");
        }
        String total="1";
        BigDecimal b1 = new BigDecimal(total);    
        BigDecimal b2 = new BigDecimal(lotteryProbability);    
        LOTTERY_PROBABILITY_DERIVATIVE=b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);    
        System.out.println("概率次数为" + LOTTERY_PROBABILITY_DERIVATIVE);
        return LOTTERY_PROBABILITY_DERIVATIVE;
    }

    // 进行比较
    public void takePrize(int rnum) {
        if (rnum > num) {
            System.out.println("随机数大于总得奖品数!没有中奖!!!!!");
        } else {
            // 奖品等级区间
            // 寻找随机数掉到什么范围内
            // pn 奖品等级范围
            long pn = num;
            Iterator<Prize> iterator = prizes.iterator();
            while (iterator.hasNext()) {
                Prize p = iterator.next();
                pn -= p.getPrizeSum();
                // 范围在递减
                if (rnum > pn) {
                    // 中奖了
                    System.out.println("中奖了!!!" + p.getPrizeName());
                    // 减少库存
                    p.setPrizeNum(p.getPrizeNum() - 1);
                    // 库存为0 删除奖品
                    if (p.getPrizeNum() == 0) {
                        iterator.remove();
                        System.out.println("抽空了");
                        System.out.println("剩余奖品:" + num + "  区间:" + odds.longValue());
                    }
                    return;
                }
            }
            
        }
    }
    
    //判断商品库是否为空
    public boolean isEmpty() {
        return prizes.isEmpty();
    }

    //刷新奖池
    public BigDecimal refresh() {
        num = 0;
        for (int i = 0; i < prizes.size(); i++) {
            num += prizes.get(i).getPrizeSum();
        }
        odds = new BigDecimal(num).multiply(LOTTERY_PROBABILITY_DERIVATIVE);
        System.out.println("随机数的范围是"+odds);
        return odds;
    }

}

3.创建main方法

package test01;

import java.math.BigDecimal;
import java.util.Random;

public class Demo {
    public static void main(String[] args) {
        GashaponMachines jackpot=new GashaponMachines();
        //输入概率大小
        jackpot.getlotteryProbabilityDerivative("0.1",10);
        
        //加入产品id
        Prize p4=new Prize("30M流量","5",10000,10000);
        Prize p3=new Prize("500M流量","30",1500,1500);
        Prize p2=new Prize("1G流量","50",1000,1000);
        Prize p1=new Prize("2G流量","70",100,100);
        //返回一个对象 (是否中奖,奖品的id(),几等奖标志)    
        //591171
        jackpot.addPrizes(p1);
        jackpot.addPrizes(p2);
        jackpot.addPrizes(p3);
        jackpot.addPrizes(p4);
        //GashaponMachines
        //抽奖区间
        jackpot.refresh();
        int i=0;
        Random r=new Random();
        while(!jackpot.isEmpty()){
            BigDecimal a = jackpot.refresh();
            i++;
            int rnum=r.nextInt(a.intValue());
            //System.out.println("第"+i+"次抽奖!!!!\t"+jackpot.getOdds());
            jackpot.takePrize(rnum);
        }

    
    }
}

 

posted on 2017-09-01 14:21  从业十余年  阅读(1048)  评论(0编辑  收藏  举报