You can write a hash function :
function hash(x, y) {
// You cast the int parts and decimal parts of your entries as 16-bits signed integers.
var xi = x & 0xFFFF;
var xf = (((x-xi) * (1 << 16)) & 0xFFFF);
var yi = y & 0xFFFF;
var yf = (((y-yi) * (1 << 16)) & 0xFFFF);
// You hash theses numbers
var r1 = ((39769 * xi) & 0xFFFF);
r1 = ((r1 + xf) * 23747) & 0xFFFF;
r1 = ((r1 + yi) * 19073) & 0xFFFF;
r1 = ((r1 + yf) * 25609) & 0xFFFF;
var r2 = ((25609 * xf) & 0xFFFF);
r2 = ((r2 + yf) * 39769) & 0xFFFF;
r2 = ((r2 + xi) * 23747) & 0xFFFF;
r2 = ((r2 + yi) * 19073) & 0xFFFF;
// And returns a floating number between 0 and 1.
return ((r1&0xFF)/(1<<24)) + ((r2&0xFFFF)/(1<<16));
}
This function wrap each 65536, as I only keep the first 16 bits of the int part, but the idea is here, you can modify the hash function. A much better way would be :
var arrayBuffer = new ArrayBuffer(8);
var dataView = new DataView(arrayBuffer);
function hash(x, y) {
dataView.setFloat32(0, x);
dataView.setFloat32(4, y);
var xi = dataView.getUint16(0);
var xf = dataView.getUint16(2);
var yi = dataView.getUint16(4);
var yf = dataView.getUint16(6);
// You hash theses numbers
var r1 = ((39769 * xi) & 0xFFFF);
r1 = ((r1 + xf) * 23747) & 0xFFFF;
r1 = ((r1 + yi) * 19073) & 0xFFFF;
r1 = ((r1 + yf) * 25609) & 0xFFFF;
var r2 = ((25609 * xf) & 0xFFFF);
r2 = ((r2 + yf) * 39769) & 0xFFFF;
r2 = ((r2 + xi) * 23747) & 0xFFFF;
r2 = ((r2 + yi) * 19073) & 0xFFFF;
// And returns a floating number between 0 and 1.
dataView.setUint16(0, r1);
dataView.setUint16(2, r2);
return Math.abs(dataView.getFloat32(0) % 1);
}
This last method use WebGL TypedArray
, which lets you have access to bits of your entries, and have a way better hash. But, from my experience, this is really slower (800 millions calls/sec for the first method on my computer, only 2 millions for the second one - for information, the classic random function is 200 millions calls/sec), and WebGL may not be available on all browsers.