1

I'm trying to create a unique unsigned "long" number ($64$ bits) from a list of $4$ other numerical values. Some function $f(n_1, n_2, n_3, n_4) = x$. The order of the values is important so unfortunately I can't use this technique (calculating unique value from given numbers) which seems to give the same unique value from a set of numbers where order doesn't matter.

For example $n_1 = 10, n_2 = 14, n_3 = 18, n_4 = 21$ should be different than $m_1 = 10, m_2 = 1, m_3 = 418, m_4 = 19$. In addition, $o_1 = 10, o_2 = 14, o_3 = 21, o_4 = 18$ should also be different.

I realize it might not be possible to get a truly unique number due to the 64-bit limitation but if you can help me to find a number that's very unlikely to be unique, that would be very very nice:)

Other interesting suggestions that I've looked at:

Function for unique hash code

Deduce a unique number from number

Calculate unique Integer representing a pair of integers

Perhaps that last one could be applied three times? Since $4$ values are two pairs, each pair could generate a unique value and then the two unique values could be used in the function again?

Kenta S
  • 18,181
span
  • 153

1 Answers1

2

The first rule of hash functions is that you probably shouldn't be designing hash functions on your own (most people design pretty bad ones).

A simple way is to take a string hash function, put the numbers in with separators, and then hash the string, like "n1,n2,n3,n4". The programming language you're using may have a built in hash function for this purpose (for example, Java has a hash function for strings built in, which you can use [or reimplement - I don't think Java requires the hash functions to be consistent on different runs of the program] but it is 32 bits). Obviously a 32 bit hash function can be mapped to 64 bits by ignoring 32 bits. Or, you can concatenate 32-bit hashes for "n1,n2" and "n3,n4" into a 64 bit long.

A list of hash functions on wikipedia may be useful.

Do you have any restrictions on what values the numbers can take?

Batman
  • 19,790
  • Thanks for your input and I realise designing hash functions for security reasons is a bad idea. I didn't realise Java's hashCode() actually returns an int. Thanks. hashCode() -> s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]. Since I'm in c++ I guess I'll build a string and use this: std::hash<std::string>()("foo");. Funny how googling can be this random after searching for 1 hour without results :P – span Feb 11 '14 at 15:49
  • Well, even ignoring security, people don't necessarily get good collision resistance. There is a Boost::hash function/std::hash function in in C++ which you may want to read up on (important to note that passing it a char * will hash the pointer, not the thing it points to, so pass it a std::string (you will obviously need ). – Batman Feb 11 '14 at 15:55
  • Again, you need to check the consistency issue for different runs of the program and across different platforms, provided you are storing these hashes. So, finding a hash function online and doing it may be the best thing to do. – Batman Feb 11 '14 at 16:03