# functions – Produce a unique integer out of list of integers with constraints

I am a computer programmer looking for a mathematical function (or a more advanced algorithm) able to produce a 32bits integer out of a list of integers with the following constraints:

• Each item in the input list is a positive integer with a known range ((0-M) with M<10, that range may differ from an element to another).
• The input list have a fixed number of element (< 10).
• The order of elements in the list is important (`(1, 2, 3)` is not the same as `(2, 3, 1)`).
• For two set of inputs, the more inputs have been modified the more different the output must be. And by this I also mean that two inputs with only one number changed must produce close outputs, no matter how much that value have changed.
• Being able to find the original list from the generated number does not matter.
• Each unique set of inputs must produce a unique output (no duplicate outputs if an input is different).
• Having not exactly the same output when taking the same input twice could be acceptable, as soon a the difference between outputs is really low (<0.1% difference).
• The range of the output numbers generated must be known (to be able to map the output values to any other range).

Here are a few examples:

• Lets say the input `(1, 2, 3)` produces the output `100` where outputs are in range `(0-1000)`.

• Then the input `(1, 42, 3)` should procude `150`, an output pretty close to it as only one number changed.

• But the input `(2, 3, 4)` should produce `500`, an output very far away as all values have changed.

I explored various answers on this website but none of them seems to match my needs:

Calculate unique Integer representing a pair of integers

Unique numerical encodings of lists of integers

“Unique” number from several values

The hash-related answers seemed promising, but sadly all those I could find appear to have huge variability for output numbers, independently of the inputs. Here controlled variability is the center of the problem.

If you are wondering what this would be used for, the idea is to use that function to produce a unique color (by mapping the output to the hue) for an object with multiple attributes, so that objects with a lot of attributes in common will look similar.

Any advice on this topic would be appreciated 🙂

Also if you think that this kind of behavior is impossible to achieve for some logical reason I completely missed, please feel free to share it!

Posted on