Results 1 to 4 of 4

Math Help - Generate random X Y Coordinates within a square/rectangle

  1. #1
    Junior Member
    Joined
    Mar 2009
    Posts
    42

    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?

    Thanks in advance.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by kerrymaid View Post
    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?

    Thanks in advance.
    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
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Mar 2009
    Posts
    42
    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;
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by kerrymaid View Post
    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.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. rectangle and square
    Posted in the Geometry Forum
    Replies: 3
    Last Post: November 11th 2011, 08:36 AM
  2. Replies: 1
    Last Post: March 28th 2011, 10:40 PM
  3. Generate a random matrix given a condition number
    Posted in the Advanced Algebra Forum
    Replies: 2
    Last Post: January 1st 2010, 08:24 AM
  4. Rhombus inside Rectangle : testing coordinates
    Posted in the Math Software Forum
    Replies: 1
    Last Post: October 11th 2008, 04:12 PM
  5. Rectangle in a square
    Posted in the Geometry Forum
    Replies: 5
    Last Post: May 30th 2006, 11:25 PM

Search Tags


/mathhelpforum @mathhelpforum