Problem
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).
- The range of the output values is considered cyclic (if output is in range
[0-1000]then0and1000would be very close output values).
Examples
Here are a few examples:
Lets say the input
[1, 2, 3]produces the output100where outputs are in range[0-1000].Then the input
[1, 42, 3]should produce150, an output pretty close to it as only one number changed.But the input
[2, 3, 4]should produce500, an output very far away as all values have changed.
Links
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.
More context
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!
EDIT:
So I did implement your function @Marcus Müller, with N=7 and Mi=6 for all elements except the first one which is 4, and it came out with a curious output numbers repartition:
Few things to notice here:
- First of all I never get value above 0.73, which means I am missing a bit more more than 1/4 of the spectrum when translating this into hues :/
- And then I get values concentrated around certain regions, leading to only a few colors appearing, and items with very different inputs still looking very similar in color...
Any clue what could have led to these results? (My implementation might of course be the main culpist here, but any lead on which part to tweak from there would help).
EDIT 2:
The legend of the graph was an inaccurate representation of the values range, and it is in fact reaching up to 0.85. Still investigating where are the missing 15% though :p