什么是限制范围输出的好散列函数
What is a good Hash Function for limit range output
我需要一个哈希函数来将 1000 个数字映射到一个 50*50 matrix.my 数字是 8 位 hex.i 使用:
static int[,] matrix = new int[50, 50];
int m=hex[0 to 3]%50;
int n=hex[4 to 7]%50;
matrix[m,n]++;
但它的功能非常糟糕,而且非常 Collision.in 事实上我会计算网络数据包中源 IP 的数量 window。
请帮我!
这 class 保证没有冲突 :-) 请注意,返回的散列是连续的。给出的第一个不同数字的哈希值将为 0,给出的第二个不同数字的哈希值将为 1,依此类推。
public class Hasher
{
private readonly Dictionary<int, int> Hashes = new Dictionary<int, int>();
public int Hash(int value)
{
int hash;
if (!Hashes.TryGetValue(value, out hash))
{
hash = Hashes.Count;
Hashes[value] = Hashes.Count;
}
return hash;
}
}
这样使用:
var hasher = new Hasher();
int hash1 = hasher.Hash(11); // 0
int hash2 = hasher.Hash(27); // 1
int hash3 = hasher.Hash(11); // 0
int hash4 = hasher.Hash(47); // 2
int hash5 = hasher.Hash(47); // 2
我需要一个哈希函数来将 1000 个数字映射到一个 50*50 matrix.my 数字是 8 位 hex.i 使用:
static int[,] matrix = new int[50, 50];
int m=hex[0 to 3]%50;
int n=hex[4 to 7]%50;
matrix[m,n]++;
但它的功能非常糟糕,而且非常 Collision.in 事实上我会计算网络数据包中源 IP 的数量 window。 请帮我!
这 class 保证没有冲突 :-) 请注意,返回的散列是连续的。给出的第一个不同数字的哈希值将为 0,给出的第二个不同数字的哈希值将为 1,依此类推。
public class Hasher
{
private readonly Dictionary<int, int> Hashes = new Dictionary<int, int>();
public int Hash(int value)
{
int hash;
if (!Hashes.TryGetValue(value, out hash))
{
hash = Hashes.Count;
Hashes[value] = Hashes.Count;
}
return hash;
}
}
这样使用:
var hasher = new Hasher();
int hash1 = hasher.Hash(11); // 0
int hash2 = hasher.Hash(27); // 1
int hash3 = hasher.Hash(11); // 0
int hash4 = hasher.Hash(47); // 2
int hash5 = hasher.Hash(47); // 2