简体   繁体   中英

I need a javascript function that maps two coordinates to a pseudo-random value between 0 and 1

Additional requirements:

  • Input of the function are two numbers, not necessarily integers

  • Speed is the major concern; it should be as fast as possible

  • Doesn't have to be secure, as long as its output looks sufficiently random.

Examples:

getValue(0,0) -> 0.326458921
getValue(100,30) -> 0.598713621
getValue(5.12687, 600.471536) -> 0.21458796

Edit To clarify: the output values should be deterministic, but random-looking.

Don't know if this fits your needs, this is just an idea I had spontaneously (using a JavaScript implementation of Java's String.hashCode(), taken from this website: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ )

<script>
    String.prototype.hashCode = function(){
        var hash = 0;
        if (this.length == 0) return hash;
        for (i = 0; i < this.length; i++) {
            char = this.charCodeAt(i);
            hash = ((hash<<5)-hash)+char;
            hash = hash & hash; // Convert to 32bit integer
        }
        return hash;
    }

    function hash(x, y) {
        var hash = Math.abs((x + "" + y).hashCode())
        return hash / Math.pow(10, (hash + "").length);
    }

    alert(hash(5.12687, 600.471536));
</script>

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.

unsigned int hash(int key) {
    key +=~(key << 15);
    key ^=(key >> 10);
    key +=(key << 3);
    key ^=(key >> 6);
    key +=~(key << 11);
    key ^=(key >> 16);
    return key;
}

unsigned int localseed = hash(x^hash(y^baseSeed));

This uses a baseSeed value, but that's optional. Basically creates a hash from x/y coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM