I get most of those types of formulas from The Aviation Formulary.

The formula he gives is:

### Lat/lon given radial and distance

A point {lat,lon} is a distance d out on
the tc radial from point 1 if:

```
lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
IF (cos(lat)=0)
lon=lon1 // endpoint a pole
ELSE
lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
ENDIF
```

This algorithm is limited to distances such that dlon < pi/2, i.e
those that extend around less than one
quarter of the circumference of the
earth in longitude. A completely
general, but more complicated
algorithm is necessary if greater
distances are allowed:

```
lat =asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
dlon=atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(lat))
lon=mod( lon1-dlon +pi,2*pi )-pi
```

Note that he's using "tc" to stand for true course (in radians clockwise from North) and the distances he gives are in radians of arc along the surface of the earth. This is explained (along with formulas to convert back and forth from nautical miles) in the first section of the Formulary. Also, check out the "Implementation Notes" and "Worked Examples" on that page.

**Incrementing / Decrementing Operators**

`++`

increment operator

`--`

decrement operator

```
Example Name Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
```

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable **first** then the result is returned. If put after the variable, the variable is **first** returned, then the increment/decrement operation is done.

For example:

```
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}
```

**Live example**

In the case above `++$i`

is used, since it is faster. `$i++`

would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use `$apples--`

, since first, you want to display the current number of apples, and **then** you want to subtract one from it.

You can also increment letters in PHP:

```
$i = "a";
while ($i < "c") {
echo $i++;
}
```

Once `z`

is reached `aa`

is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.

**Stack Overflow Posts:**

## Best Solution

@MikeLewis answer is by far a simpler approach, but it only gives you a range of latitude and longitude, and drawing randomly from that might give you points outside the given radius.

The following is a bit more complicated, but should give you 'better' results. (The chances are that isn't necessary, but I wanted to have a go :) ).

As with @MikeLewis' answer the assumption here is that Earth is a sphere. We use this not only in the formulas, but also when we exploit rotational symmetry.

## The theory

First we take the obvious approach of picking a random distance

`$distance`

(less then`$radius`

miles) and try to find a random point`$distance`

miles away. Such points form a circle on the sphere, and you can quickly convince yourself a straightforward parametrisation of that circle is hard. We instead consider a special case: the north pole.Points which are a set distance away from the north pole form a circle on the sphere of fixed latitude (

`90-($distance/(pi*3959)*180`

). This gives us averyeasy way of picking a random point on this circle: it will haveknownlatitude andrandomlongitude.Then we simply rotate the sphere so that our north pole sits at the point we were initially given. The position of our random point after this rotation gives us the desired point.

## The code

Note:The Cartesian <--> Spherical co-ordinate transformations used here are different to what is usual in literature. My only motivation for this was to have the z-axis`(0,0,1)`

was pointing North, and the y-axis`(0,1,0)`

pointing towards you and towards the point with latitude and longitude equal to 0. So if you wish to imagine the earth you are looking at the Gulf of Guinea.