DJB 哈希函数中数字 5381 的原因? [英] Reason for the number 5381 in the DJB hash function?

查看:30
本文介绍了DJB 哈希函数中数字 5381 的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么在 DJB 哈希函数中使用数字 5381?

Can anyone tell me why the number 5381 is used in the DJB hash function?

DJB 哈希函数定义为:

The DJB hash function is defined as:

  • h 0 = 5381

h i = 33h i - 1 + s i

h i = 33h i - 1 + s i

这是一个 C 实现:

unsigned int DJBHash(char* str, unsigned int len)
{
   unsigned int hash = 5381;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++)
   {   
      hash = ((hash << 5) + hash) + (*str);
   }   

   return hash;
}

推荐答案

5381 只是一个数字,在测试中,结果 更少的碰撞更好的雪崩.您几乎可以在每个哈希算法中找到魔法常数".

5381 is just a number that, in testing, resulted in fewer collisions and better avalanching. You'll find "magic constants" in just about every hash algo.

这篇关于DJB 哈希函数中数字 5381 的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆