Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

红包算法测试问题 #146

Open
1yus2yus3 opened this issue Mar 6, 2019 · 2 comments
Open

红包算法测试问题 #146

1yus2yus3 opened this issue Mar 6, 2019 · 2 comments
Labels

Comments

@1yus2yus3
Copy link

1yus2yus3 commented Mar 6, 2019

根据您的红包实现算法,自己测试了一下数据,产生200W个红包 每个红包的总金额都是200 分给10个人,按理说在每个人获得的金额在概率分布中应该是20元左右,但是测试出来的结果 有同一个共性,就是 倒数 后面几个人获取的平均值 总是距离平均值很远。从概率的角度想 不应该出现这个问题吧
bug

@crossoverJie
Copy link
Owner

@1yus2yus3

请把你测试的这段代码贴下,我模拟试试。

@1yus2yus3
Copy link
Author

1yus2yus3 commented Mar 8, 2019

public static void main(String[] args) {

    Map<Integer,List<Integer>> map = new HashMap();
    for (int i = 0; i < 2000000; i++) {
        //产生红包
        RedPacket redPacket = new RedPacket();
        List<Integer> redPackets = redPacket.splitRedPacket(20000, 10);

        //System.out.println(redPackets);
        for (int j = 0; j < redPackets.size(); j++) {
            if(map.get(j) == null) {
                map.put(j,new ArrayList<Integer>());
            }
            map.get(j).add(redPackets.get(j));
        }
    }

    //对map进行数据统计
    Iterator<Map.Entry<Integer, List<Integer>>> entryIterator = map.entrySet().iterator();
    while (entryIterator.hasNext()) {
        Map.Entry<Integer, List<Integer>> next = entryIterator.next();

        List<Integer> values =  next.getValue();
        IntSummaryStatistics stats = values.stream().mapToInt((x)->x).summaryStatistics();
        System.out.println("第" + (next.getKey() + 1) + "个人获取到 平均值=" + stats.getAverage());
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants