抽奖

package com.java.easy.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import com.alibaba.fastjson.JSON;


public class LotteryUtil {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Gift> list = new ArrayList<Gift>();
        list.add(new Gift("red", 0.3));
        list.add(new Gift("white", 0.2));
        list.add(new Gift("yellow", 0.1));
        list.add(new Gift("red", 0.3));
        list.add(new Gift("black", 0.1));
        list.add(new Gift("blue", 0.4));
        list.add(new Gift("orign", 0.5));
        
        //通用,即使总概率大于1的情况
        double total = 0;
        for(Gift gift:list) {
            total += gift.getProbability();
        }
        for(Gift gift:list) {
            gift.setProbability(gift.getProbability()/total);
        }
        //算每一个元素的概率区间
        for(int i=0;i<list.size();i++) {
            if(i==0) {
                list.get(i).setMin(0);
                list.get(i).setMax(list.get(i).getProbability());
            }else {
                list.get(i).setMin(list.get(i-1).getMax());
                list.get(i).setMax(list.get(i-1).getMax()+list.get(i).getProbability());
            }
        }
        int count = 100000;
        for(int i=0;i<count;i++) {
            double gen = Math.random();
            for(Gift gift:list) {
                if(gen >= gift.getMin() && gen < gift.getMax()) {
                    gift.setCount(gift.getCount()+1);
                    gift.setActProbability((double)gift.getCount()/count);
                    break;
                }
            }
        }
        System.out.println(JSON.toJSONString(list));
    }
    
    static class  Gift{
        private String color;
        private double probability;//概率
        private double min; //概率区间最小值
        private double max;//概率区间最大值
        
        private double actProbability;//实际概率
        private int count;//中彩次数
        public String getColor() {
            return color;
        }
        public Gift (String color, double probability)
        {
            super();
            this.color = color;
            this.probability = probability;
        }
        public int getCount() {
            return count;
        }
        public void setCount(int count) {
            this.count = count;
        }
        public double getActProbability() {
            return actProbability;
        }
        public void setActProbability(double actProbability) {
            this.actProbability = actProbability;
        }
        public void setColor(String color) {
            this.color = color;
        }
        public double getProbability() {
            return probability;
        }
        public void setProbability(double probability) {
            this.probability = probability;
        }
        
        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if(obj == null) {
                return false;
            }
            if(obj == this) {
                return true;
            }
            if(this.getClass() != obj.getClass()) {
                return false;
            }
             if(obj instanceof Gift) {
                 Gift gift =(Gift) obj;
                 return gift.getColor().equals(this.getColor());
             }
            return false;
        }
        
       public double getMin() {
            return min;
        }
        public void setMin(double min) {
            this.min = min;
        }
        public double getMax() {
            return max;
        }
        public void setMax(double max) {
            this.max = max;
        }
    @Override
        public int hashCode() {
            // TODO Auto-generated method stub
            return this.getColor().hashCode();
        }
        
    }

}

//抽奖100000次输出结果:

[
  {
    "actProbability": 0.15805,
    "color": "red",
    "count": 15805,
    "max": 0.15789473684210525,
    "min": 0,
    "probability": 0.15789473684210525
  },
  {
    "actProbability": 0.10486,
    "color": "white",
    "count": 10486,
    "max": 0.2631578947368421,
    "min": 0.15789473684210525,
    "probability": 0.10526315789473685
  },
  {
    "actProbability": 0.05406,
    "color": "yellow",
    "count": 5406,
    "max": 0.3157894736842105,
    "min": 0.2631578947368421,
    "probability": 0.052631578947368425
  },
  {
    "actProbability": 0.15851,
    "color": "red",
    "count": 15851,
    "max": 0.47368421052631576,
    "min": 0.3157894736842105,
    "probability": 0.15789473684210525
  },
  {
    "actProbability": 0.05199,
    "color": "black",
    "count": 5199,
    "max": 0.5263157894736842,
    "min": 0.47368421052631576,
    "probability": 0.052631578947368425
  },
  {
    "actProbability": 0.2115,
    "color": "blue",
    "count": 21150,
    "max": 0.7368421052631579,
    "min": 0.5263157894736842,
    "probability": 0.2105263157894737
  },
  {
    "actProbability": 0.26103,
    "color": "orign",
    "count": 26103,
    "max": 1,
    "min": 0.7368421052631579,
    "probability": 0.2631578947368421
  }
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值