'How to simulate distances from a fixed point to random points within a given radius in R?
I am trying to simulate N distances between a fixed point and other points randomly distributed around it within a given radius.
One way I've thought of is to simulate coordinates for the random points, then calculate the distances, then exclude distances greater than the given radius (say r = 250m):
X <- runif(N, -250, 250) # simulate random X coordinate
Y <- runif(N, -250, 250) # simulate random Y coordinate
distance <- sqrt(X^2 + Y^2) # calculate distance from random points to center
distance <- distance[distance < 250] # only include values within given radius
However, I am wondering if there is a way to simulate these distances without simulating the coordinates themselves. My end goal is to be able to do this in JAGS so solutions that work in JAGS are preferred. Is there a probability distribution that could be used to describe the probability of these distances to random points? An ideal solution would look something like this:
distance ~ pDistribution(N, 250)
or alternatively in JAGS:
for (1 in 1:N) {
distance[i] ~ pDistribution(250)
}
Solution 1:[1]
@jlhoward had a good idea with thinking in polar coordinates - that's what got me going in the right direction. However, by using r = runif(250), you would end up with points clustered around the center. To have a uniformly random distribution of points throughout the circle, there must be more points at greater distances from the center (because circumference/area increase with radius). Turns out you can do this with r <- 250 * sqrt(runif(N, 0, 1)). For my problem, all I needed was to generate these distances (i.e., radii), not the actual points, so this code is an adequate solution. This great video on finding random points in a circle is what helped me finally figure it out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dankdweb |
