# Generate random X Y Coordinates within a square/rectangle

• Dec 6th 2010, 04:41 AM
kerrymaid
Generate random X Y Coordinates within a square/rectangle
Hi,

I'm a computer programmer and am currently coding up an algorithm. As part of the algorithm I need to take a unique ID (either a series of characters or most likely a large integer) and generate an X,Y pair from this ID within a given square/rectangle (I know the co-ordinates of each 4 boundary coordinates of the square/rectangle). These generated X,Y coordinates need to be uniformly distributed within the area and also unique so that every time I do this conversion on a particular ID I always get the same resulting X,Y pair and a conversion on any other ID does not result in the same X,Y pair.

Can anyone help me with how to approach this?

• Dec 7th 2010, 04:21 AM
CaptainBlack
Quote:

Originally Posted by kerrymaid
Hi,

I'm a computer programmer and am currently coding up an algorithm. As part of the algorithm I need to take a unique ID (either a series of characters or most likely a large integer) and generate an X,Y pair from this ID within a given square/rectangle (I know the co-ordinates of each 4 boundary coordinates of the square/rectangle). These generated X,Y coordinates need to be uniformly distributed within the area and also unique so that every time I do this conversion on a particular ID I always get the same resulting X,Y pair and a conversion on any other ID does not result in the same X,Y pair.

Can anyone help me with how to approach this?

Use your ID to seed a psudo-random number generator, use it to generate a pair of random numbers x, y uniform on the unit interval, and then map the pair (x,y) to a point in the desired rectangle (X,Y) in the obvious manner.

CB
• Dec 17th 2010, 04:22 AM
kerrymaid
Hi CB,

Ok, I did this to a point by passing in a unique ID, seeding the random number generator, generating the two numbers and then scaling them (see below). The issue is that when I pass in the same ID again later I get two different numbers...This makes sense since this is what a random number generator is supposed to do in the first place but I suppose I'm a bit confused as to a way around it.

Thanks again.

if (GHLSrnGenCreated == OPC_FALSE)
{
ghls_randomGenPtr = prg_random_gen_create(intndID);
GHLSrnGenCreated = OPC_TRUE;
}

//variant in the range [0,1]
x = prg_random_double_gen(ghls_randomGenPtr);
y = prg_random_double_gen(ghls_randomGenPtr);

//x value:
hashedPosCoordinates->current_longitude = x * area_size_x;
//y value:
hashedPosCoordinates->current_latitude = y * area_size_y;
• Dec 17th 2010, 04:43 AM
CaptainBlack
Quote:

Originally Posted by kerrymaid
Hi CB,

Ok, I did this to a point by passing in a unique ID, seeding the random number generator, generating the two numbers and then scaling them (see below). The issue is that when I pass in the same ID again later I get two different numbers...This makes sense since this is what a random number generator is supposed to do in the first place but I suppose I'm a bit confused as to a way around it.

Thanks again.

if (GHLSrnGenCreated == OPC_FALSE)
{
ghls_randomGenPtr = prg_random_gen_create(intndID);
GHLSrnGenCreated = OPC_TRUE;
}

//variant in the range [0,1]
x = prg_random_double_gen(ghls_randomGenPtr);
y = prg_random_double_gen(ghls_randomGenPtr);

//x value:
hashedPosCoordinates->current_longitude = x * area_size_x;
//y value:
hashedPosCoordinates->current_latitude = y * area_size_y;

The whole purpose of a seed is to generate the same sequence of numbers when you repeat the process with the same seed. So there is something wrong with the seeding method, maybe you have to replicate the whole state vector of the prng, you will have to look at the documentation of the number generator to see what it is doing.