algorithm - Evenly distributed hash function -


i need hash function takes few (eg. 2 or 3) unsigned integers input, , returns floating point value between -1 , +1.

the collection of these returned values must evenly distributed. sequence of outputs function must appear random sequence, if input numbers sequential. faster better, i'm calling lot of times.

i hope isn't ask :s...

you can employ standard scheme such tasks: (a0 + q*a1 + q^2*a2 + q^3*a3 + ...) % m m large prime number , q coefficient of choice.
once have random enough hash in range [0, m), converting floating point number [-1, 1] trivial.

or can remove % m , allow integer overflow happen, although i'm not sure how secure (from 'evenly distributed' perspective).

a sequence of outputs function must appear random sequence, if input numbers sequential.
can instead of ai use ai*ai in expression. anyway, here's simple implementation in java.

double hash(int... a) {     int q = 433494437;     int result = 0;     (int n : a) {         result = result * q + n * n;     }     result *= q;     return (double) result / integer.min_value; } 

output random consecutive numbers. can use 64-bit integer more precision.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -