You can employ standard scheme for such tasks: (a0 + Q*a1 + Q^2*a2 + Q^3*a3 + ...) % M
where M
is a very large prime number and Q
is coefficient of your choice.
Once you have random enough hash in range [0, M)
, converting it to floating point number [-1, 1]
is trivial.
Or you can remove % M
and allow integer overflow to happen, although I'm not sure how secure it is (from 'evenly distributed' perspective).
A sequence of outputs from the function must appear to be a random sequence, even if the input numbers are sequential.
For this you can instead of ai
use ai*ai
in the expression. Anyway, here's the simple implementation in Java.
double hash(int... a) {
int Q = 433494437;
int result = 0;
for (int n : a) {
result = result * Q + n * n;
}
result *= Q;
return (double) result / Integer.MIN_VALUE;
}
Output does look random even for consecutive numbers. You can also use 64-bit integer for more precision.