模拟概率抽奖

package com.startx.http.wordfilter;


import java.util.*;

public class RandomDemo {


    public static void main(String[] args) {
        Map<String, Object> maps = new LinkedHashMap<>();
        maps.put("0.1", "0.1111");
        maps.put("1.5", "111111");
        maps.put("5", "555555");
        maps.put("30", "33333333333333");
        maps.put("60", "66666666666666");
        maps.put("90", "99999999999999");
        maps.put("100", "00000000000000");


        for (String s : maps.keySet()) {
            String key = getTarget(s);
            System.err.println("key = ===" + key);
            if (key != null) {
                System.err.println("value ============" + maps.get(key));
                break;
            }
        }

    }


    /**
     * 命中则将key返回
     *
     * @param p
     * @return
     */
    public static String getTarget(String p) {


        //单位转换称整数
        int conversion = 10;

        //挑选到的数据
        List<Integer> userList = new LinkedList<>();

        //happy 号码
        Integer happy = new Random().nextInt(100);


        //几率
        Double probability = new Double(p);

        String probabilityString = probability.toString();

        int total = 100;


        int douat;
        if ((douat = probabilityString.indexOf(".")) != -1) {
            int sum = probabilityString.length();
            //不是整数
            if (!(sum - 2 == douat && probabilityString.charAt(douat + 1) == '0')) {
                if (new Integer(probabilityString.substring(0, douat)) > 100) {
                    System.err.println(" probability   is error!!!");
                    return null;
                }

                for (int i = 0; i < sum - douat - 1; i++) {
                    total *= conversion;
                    probability *= conversion;
                }
            }
        }

//        5%概率在100个数字中连续拿5个数字

            Set<String> targetSet = new HashSet<>();
        for (int i = 0; i < probability; i++) {
            //设置的获取成功的目标值
            Integer target;
            //判断数值是否在之前已经使用过
            while (true) {
                target = new Random().nextInt(total);
                if (!targetSet.contains(target.toString())) {
                    targetSet.add(target.toString());
                    break;
                }
            }
            userList.add(target);
            if (i == probability - 1) {
                System.err.println("happy=======" + happy);
                System.err.println("选择的" + userList);
                System.err.println("size=======" + userList.size());
            }
            if (target == happy) {
                System.err.println("happy=======" + happy);
                System.err.println("选择的" + userList);
                System.err.println("size=======" + userList.size());
                System.err.println("good luck!");
                return p;
            }
        }
        return null;
    }
}

  • List item
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值