散列函数中的均匀分布

问题描述

我编写了以下基本散列函数来处理散列图数据结构:

static int ht_hash(const char* s,int prime,int num_buckets)
{
    long hash = 0;
    size_t len_s = strlen(s);
    for (int i=0; i < len_s; i++)
    {
        // 1. left-most digit should be prime^^len-1,right-most should be prime^^0
        long prime_to_power = (long) pow(prime,len_s - (i+1));
        // 2. multiple that value for the digit times the code (asci code in this case) for that char
        hash += prime_to_power * s[i];
    }
    // 3. hash / modulo
    hash %= num_buckets;
    return (int) hash;

}

如何确定上述基本散列函数的“好”桶大小或“素数”?我知道目标是均匀分布。但是,除了使用 100k 值进行蛮力之外,还有其他方法可以做到这一点,例如:

for (int num_buckets=0; num_buckets < 100000; num_buckets++)
    // for (int prime=1; prime < 10000; prime=get_next_prime())
    // or,if we want to grab all numbers,not just prime and see for ourselves
    for (int number=1; number < 100000; number++)

        // ... store some histogram-like data

或者,这是否更像是一个抽象的数学领域,其中有证据显示均匀分布的可能条件/约束?换句话说,什么是了解散列函数及其实现方式的好方法?以及如何确认它们是统一的?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)