package com.yujian.util;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @author yujianjian
 * @CreateTime 2017-02-15 上午9:40
 * @Description RandomProbablityUtil.java
 */
public class RandomProbablityUtil {

    private static Map<Integer, Integer> map = Maps.newHashMap();

    static {//因为math.random函数能够取到0不到取到1的原因
        map.put(1, 0);//数据库里可以配置为1取出来的时候判断为1就用0替代
        map.put(2, 7999);
        map.put(3, 1000);
        map.put(4, 900);
        map.put(5, 50);
        map.put(6, 30);
        map.put(7, 15);
        map.put(8, 5);
        int count = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            count += entry.getValue();
        }
        if(count != 9999){
            System.err.println("error,总权重不等于9999设置概率错误");
        }
    }


    private static int percentageRandom() {
        return  (int)(Math.random() * 10000);
    }


    public static void main(String[] args) {
        for(int i=0;i<=10000;i++){
            int random = percentageRandom();
            if(random == 9999){
                System.err.println("能够取到极限值random = "+random);
            }else{
                //System.out.println(random);
            }
        }

    }
}
