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的一致性哈希算法

散列函數與分流算法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章