# How can I generate random coordinates for left, right, bottom, and top positions?

So what Im trying to do is generate 4 random coordinates between the screen size of an android device depending on whether I want the coordinates to be positioned in the top half of the screen or the bottom half.

See the graph(image) here:

https://stackoverflow.com/questions/11483345/how-do-android-screen-coordinates-work

Say I have the maxX and maxY coordinates of the device, like shown in the above link ^^ How would I go about creating the correct coordinates that are positioned in the topic half and top bottom of the screen?

Is there some formula to finding this?

An example of high and low would be (screen size): maxX=800, maxY=1280 | If anyone knows the java language, here is a snippet of code.

private static int[] generateRandomNumbers(int high, int low){
int[] numbers = new int[4];
int R;

int Low = low;
int High = high;

for(int i=0;i<numbers.length;i++) {
Random r = new Random();
R = r.nextInt(High - Low) + Low;
numbers[i] = R;
}
return numbers;
}


## Edit:

### As written in the comments:

I want to generate 4 numbers (coordinates) within the top half or the bottom half of a phone screen (rectangle), but I only want to generate one or the other. E.g if the user swipes up I'll run a method that will generate 4 numbers (coordinates) that are in the TOP half of the screen. If they swipe down I'll do the opposite.

So say if the the screen height and width was 720by1280 I'd need the 4 numbers (coordinates) to be within that range. Am I making sense? Im not so good at math, so it's hard for me to explain it in mathematical terms.

• Um, what is your question? Commented Oct 26, 2015 at 7:49
• I added more detail. I basically need to get coordinates in either the top or bottom half of the screen (780x1280). I was thinking there would be some sort of formula to generate a coordinate if you had the screen size. @HenningMakholm At the moment I've got a bit of java code that generates random numbers between x and y Commented Oct 26, 2015 at 8:19
• You are still not being clear what it is that you want. Do you want to select four random points within a specified rectangle? And what's going on with the division into top/bottom of the screen. How many points do you want in the top and how many in the bottom? Commented Oct 26, 2015 at 8:29
• Well I want to generate 4 numbers (coordinates) within the top half and the bottom half of a phone screen (rectangle), but I only want to generate one or the other. E.g if the user swipes up i'll run a method that will generate 4 numbers in the TOP half of the screen. If they swipe down I'll do the opposite. So say if the the screen height and width was 720by1280 I'd need the 4 numbers (coordinates) to be within that range. Am I making sense? Im not so good at math so it's hard for me to explain it in mathematical terms. @Donkey_2009 Commented Oct 26, 2015 at 8:36

You already have a method that will return you four random numbers within a specified range.

An integer point in your rectangle is given by coordinates $$(x, y)$$ where $1\le x\le\text{MaxX}$ and $1\le y\le\text{MaxY}$.

It's not completely obvious why this should be the case, but uniformly choosing a random such coordinate is equivalent to uniformly choosing $x$ randomly and then uniformly choosing $y$ randomly. Then the probability of choosing any given pair $(x,y)$ is

$$\frac{1}{\text{MaxX}\text{MaxY}}$$

I.e., it is the reciprocal of the total number of integer points in the rectangle.

As an example of choosing four points at random within this rectangle:

int[] xCoordinates = generateRandomNumbers(1, MaxX);
int[] yCoordinates = generateRandomNumbers(1, MaxY);

java.awt.point[] randomPoints;

for (int i = 0 ; i < 4 ; ++i) {
randomPoints[i].x = xCoordinates[i];
randomPoints[i].y = yCoordinates[i];
}


Of course, a better solution might be to modify your generateRandomNumbers method so that it returns points rather than numbers.

Now you might want to choose four random points in the top half of the plane. In that case, you only need to modify the parameters used when choosing the y coordinates - so you would use

int[] yCoordinates = generateRandomNumbers(1, MaxY / 2);


instead, while if you wanted to choose four random points in the bottom half of the plane, you'd use

int[] yCoordinates = generateRandomNumbers((MaxY / 2) + 1, MaxY);

• Thanks so much for explaining everything in detail! Helped me understand the proper way of doing this! Commented Oct 26, 2015 at 8:55