# How to generate two random where the distance between is within a range

9 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

John D'Errico
on 8 Dec 2021

Can you "say" that? Um, yes. But then you need to write the code.

Given one known point, now you want to find another randomly that is randomly somewhere 10-60m away from it. The set of points that are LESS then 10m lie in a circle around your center (of radiuus 10m). And the set of points that are MORE than 60m lie outside of another circle, this one of radius 60m.

Therefore the set of points that are admissable form a circular annulus around your center, thus a ring, with inner radius of 10m, and outer radius 60m. If you assume the points are distributed uniformly and randomly inside that region, then your question is apparently how would you choose a new point inside that region?

The simple scheme is to use rejection. For example, keep on generating random points inside the larger circle, but rejecting all points that lie inside the inner circle. As soon as you find a point that satisfies both constraints, you are done. This will not be too inefficient. Of course, you need to know how to generate points inside a circle, and how to do so uniformly. And then you need to know how to test if a point lies inside a circle. (That seems trivial to me, but surprisingly not to everyone.)

A better scheme will generate random values for the radius, inside the interval [10m,60m]. The problem is, you must not do that uniformly, but according to the correct distribution. Once you have chosen a proper random radius, now you choose a uniformly random angle from 0-2*pi. Given that, you convert from polar coordinates to cartesian. Finally, add the coordinates of the cetner point, and you have a randomly generated point inside a circle.

Personally, I'd use the latter scheme.

##### 0 Comments

### More Answers (1)

Rik
on 8 Dec 2021

If you need a uniform distribution you might need to randomly generate pairs of deltaX and deltaY until hypot(deltaX,deltaY) is in your desired range.

If a non-uniform distribution is fine, you can randomly generate a distance and an angle, after which pol2cart allows you to find the corresponding deltaX and deltaY. This will result in a higher density closer to your first point.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!