How to generate two random where the distance between is within a range
6 次查看(过去 30 天)
显示 更早的评论
Let's say we already generate a point randomly,the coordinates of it is fixed, so how to generate anothe randomly which is 10m-60m far away from it ? Can i say the point i want to generate is at a position between two circles whose center are both the previous point?
0 个评论
采纳的回答
John D'Errico
2021-12-8
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 个评论
更多回答(1 个)
Rik
2021-12-8
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!