# Generating random points near a location [closed]

In the following solution, why is there a sqrt(u)? Since u is just a random number in [0, 1) doesn't getting the sqrt just give you another smaller random number?

To generate points uniformly, randomly, and independently within a circle of radius r around a location (x0, y0), start by generating two independent uniform random values u and v in the interval [0, 1). (This is what almost every random number generator provides you.) Compute

``````d = r * sqrt(u)

theta = 2 * Pi * v

x = d * cos(theta)

y = d * sin(theta)
``````

The desired random point is at location (x+x0, y+y0).

• What you're asking is not really clear. You should provide more context to your question (for example, what do t and w represent?). Also, this is probably more a question for math.stackexchange.com. – ArMoraer Mar 22 '16 at 15:54
• @ArMoraer Renamed variables to make it clearer. – aleph2012 Mar 22 '16 at 15:57
• I'm getting it from here: gis.stackexchange.com/questions/25877/… Everybody just seemed to accept the sqrt, as if it were obvious why it was there! – aleph2012 Mar 22 '16 at 16:02
• This is clearer indeed. I don't have any proper explanation, only a simple thought: if you just define `d` as `r*u`, then half of your random points will fall inside a circle of radius `r/2`, which has an area 4 times smaller than a circle of radius `r`. This behavior is not consistent with a uniform random law. So I guess the `sqrt` is there to somehow correct this spatial repartition biais. – ArMoraer Mar 22 '16 at 16:04
• Doesn't that give you all angles between 0 and 360 degrees? – aleph2012 Mar 22 '16 at 16:20

Your first two equations choose a random distance and a random angle. Your random numbers will range from [0,1] uniformly. You can look at the rings below as limiting the choices to 0.1, 0.2, etc. If you don't alter the possible distribution of distances (left), there are many more chances to be quite close to the center. The probabilities are much more uniform in the situation on the right, weighting by the square root. It is not entirely clear as to what you are after. If your intent is to create a sample within a radius and exclude an inner radius, there is a function "sample.annulus" in the spatialEco R package that will create a sample given a defined inner and outer radius. The original intent of the function was to create distance lagged samples.

First we add required libraries, example data and subset point location

``````library(sp)
library(spatialEco)
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")
xy <- meuse[2,]
``````

Now, we run the sample.annulus function at two scales and plot results

``````rs100 <- sample.annulus(xy, r1=50, r2=100, n = 50, type = "random")
rs200 <- sample.annulus(xy, r1=100, r2=200, n = 50, type = "random")

plot(rs200, pch=20, col="red")
points(rs100, pch=20, col="blue")
points(xy, pch=20, cex=2, col="black")
box()
legend("topright", legend=c("50-100m", "100-200m", "source"),
pch=c(20,20,20), col=c("blue","red","black"))
``````