crc32 散列均衡

应用场景中,需要让一些唯一的数据,散列均匀的分布在不同的桶中,或者hash槽中,从而可以验证一些AB test 场景。

 

public static void main(String[] args) {

        List<Long> list = Lists.newArrayList();
        List<Long> list0 = Lists.newArrayList();
        List<Long> list1 = Lists.newArrayList();
        List<Long> list2 = Lists.newArrayList();
        List<Long> list3 = Lists.newArrayList();
        Random random = new Random(10000);
        for (int i = 0; i < 100000; i++) {
            Integer ri = random.nextInt(10000);
            String input = ri + "";

            CRC32 crc32 = new CRC32();
            crc32.update(input.getBytes());
            long value = crc32.getValue();

            long a = value % 4;
            if (a == 0L) {
                list0.add(value);
            } else if (a == 1L) {
                list1.add(value);
            } else if (a == 2L) {
                list2.add(value);
            } else if (a == 3L) {
                list3.add(value);
            } else {
                list.add(value);
            }

        }


        BigDecimal bd = new BigDecimal("100000");
        System.out.println(list0.size()+" : "+ new BigDecimal(list0.size() + "").divide(bd));
        System.out.println(list1.size()+" : "+ new BigDecimal(list1.size() + "").divide(bd));
        System.out.println(list2.size()+" : "+ new BigDecimal(list2.size() + "").divide(bd));
        System.out.println(list3.size()+" : "+ new BigDecimal(list3.size() + "").divide(bd));
        
    }

桶数据:桶占比

25137 : 0.25137
24822 : 0.24822
25071 : 0.25071
24970 : 0.2497

 

推荐两篇不错的分析:
[hash] -- 分布均匀的hash函数

jump Consistent hash:零内存消耗,均匀,快速,简洁,来自Google的一致性哈希算法

散列函数与分流算法

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值