lundi 4 mai 2015

Creating a hash / seed value from 2+ integers (fast)

I'm working on generating different types of Gradient Noise. One of the things that this noise requires is the generation of random vectors given a position vector.

This position vector could be anything from a single int, or a 2D position, 3D position, 4D position etc.

On top of this, an additional "seed" value is needed.

What's required is a hash of these n+1 integers into a unique integer with which I can seed a PRNG. It's important that it's these values as I need to be able to retrieve the original seed every time the same values are used.

So far I've tried an implementation of Fowler–Noll–Vo; but it was way too slow for my purposes.

I've also tried using successive calls to a pairing function:

int pairing_function(int x, int y)
{
    return(0.5*(x+y)*(x+y+1) + x);
}

I.e.:

int hash = pairing_function(pairing_function(x,y),seed);

But what seems to happen is that with a large enough seed, the values overflow the size of an int (or even larger types).

What's a good method to achieve what I'm trying to do here? What's important is speed over any cryptographic concerns as well as not returning numbers larger than my original data types.

I'm using C++ but so long as any code is readable I can nut it out.




Aucun commentaire:

Enregistrer un commentaire