jeudi 5 mai 2016

Two-way hashing of fixed range numbers

I need to create a function which takes a single integer as argument in the range 0-N and returns a seemingly random number in the same range.

Each input number should always have exactly one output and it should always be the same.

Such a function would produce something like this:

f(1) = 4
f(2) = 1
f(3) = 5
f(4) = 2
f(5) = 3

I believe this could be accomplished by some kind of a hashing algorithm? I don't need anything complex, just not something too simple like f(1) = 2, f(2) = 3 etc.

The biggest issue is that I need this to be reversible. E.g. the above table should be true left-to-right as well as right-to-left, using a different function for the right-to-left conversion is fine.

I know the easiest way is to create an array, shuffle it and just store the relations in a db or something, but as I need N to be quite large I'd like to avoid this if possible.

Any ideas?




Aucun commentaire:

Enregistrer un commentaire