Hash an ID to a Random x,y coordinate within a boundary square
I have a defined area (I know the x,y coordinates of each corner of the area). I need to hash an ID开发者_JAVA技巧 (int or char array) to an x,y point confined within that boundary area. The resulting hashed value i.e. x,y co-ordinates need to be unique to the ID and preferably I don't want the x,y points clustered all in one part of the square but rather evenly distributed over the square.
Any advice about how I can go about this?
Many thanks in advance.
First, I don't think you can guarantee uniqueness. If you have maxX*maxY+1
IDs, you will necessarily have two IDs at the same coordinate.
Second, to avoid clustering, use a cryptographically secure hash.
Third, to get x and y coordinates (roughly / pseudocode):
hashResult = hash(ID)
x = hashResult modulo maxX.
y = ( hashResult div maxX) modulo maxY
精彩评论