Generate random boxed co ordinates in c#

I need to find random co-ordinates, bound like a square. For this I defined this where the values 70, 55, 175, 175 are the furthest points I want to go to:

north = Utility.generateRandomNumber(Utility.Directions.NORTH, 70);
south = Utility.generateRandomNumber(Utility.Directions.SOUTH, 55);
east = Utility.generateRandomNumber(Utility.Directions.EAST, 175);
west = Utility.generateRandomNumber(Utility.Directions.WEST, 175);

My generator is below, where I have declare a global static param:

public static Random random = new Random();

Directions is an enumerator.

public static int generateRandomNumber(Directions direction, int to)
{
    if ((direction == Directions.SOUTH) || (direction == Directions.WEST))
        return random.Next(to * -1, 0);
    else
        return random.Next(0, to);
}

The function works fine and I retrieve co-ordinates like below:

North: 52 South: -13 East: 82 West: -105
North: 27 South: -45 East: 172 West: -117
North: 0 South: -37 East: 161 West: -160
North: 43 South: -39 East: 26 West: -174
North: 29 South: -7 East: 75 West: -125
North: 19 South: -51 East: 93 West: -49
North: 28 South: -20 East: 26 West: -28

The issue is that the box is built around the (0,0,0,0) co-ordinate and I'm not sure how to get out of it whilst ensuring that North is greater than south, west is left to the map and east is right to the map.

Jon Skeet
people
quotationmark

I would suggest you change your approach:

  • Generate a point for the North/West corner, anywhere in the appropriate range
  • Generate the width and height, ensuring they're positive
  • Set East = West + Width, and South = North - Height

people

See more on this question at Stackoverflow