Daniel Daniel - 1 year ago 114
Java Question

Create unique numeric ID from multiple numeric IDs

What is a good algorithm to generate a unique 64-bit ID starting from multiple numeric 64-bit IDs? Example:

Input: [2, 9875, 0, 223568, ...] a list of random 64-bit IDs

Output: a unique 64-bit numeric ID, that have to be the same for the given input

I'm looking a way to avoid ID collision.

My apologies for the unclear question.

Answer Source

If speed does not matter, what about:

feeding all your ids in the md5-algorithm and than simply use

a) first 64 bits or b) last 64 bits or c) first 64 bits xor last 64 bits

If speed matters

What about:

Step 1: reorder the bytes of all 64 bit IDs (in a fixed but different order for each 64 bit ID of your Input.)(This might help a bit if the values are not really randomly distributed)

Step 2: xor all the rearranged 64 bit IDs to get the new 64 bit id.

If you have no extra information about the range of your 64 bit input IDs or the distribution of the values, there is no way to avoid collisions in a 'clever'/'best' way. Because whatever you come up with, you will always find a set of inputs which lead to collisions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download