随机选择平面上的点,距离较近的点选择的概率较高
我有一个问题不太确定如何解决。我有一个二维空间,里面有几个点。我还有一个当前点,它是空间中的点之一。我想随机选择另一个点之一,选择更接近我当前点的点的概率更高。我在 Java 工作。任何提示将不胜感激。
I've got a problem I'm not too sure how to solve. I have a 2d space with several points in it. I also have a current point, which is one of the points in the space. I want to randomly select one of the other point, with a higher probability for selection being given to points closer to my current point. I'm working in Java. Any tips would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过例如计算
1 / distanceFromCurrent
为每个点分配“权重”。根据这些权重选择一个点。
例如,后一部分的解决方案可以在以下一些答案中找到:
另一种选择是使用 < a href="http://download.oracle.com/javase/6/docs/api/java/util/Random.html#nextGaussian%28%29" rel="nofollow noreferrer">
java.util. Random.nextGaussian
。调整生成的双精度值,使其代表合理的半径,然后选择最接近该半径的相邻点。Assign a "weight" to each point by for instance computing
1 / distanceFromCurrent
.Select a point based on these weights.
Solution for the latter part can for instance be found in some of the following answers:
Another option would be to use
java.util.Random.nextGaussian
. Adjust the resulting double so that it represents a reasonable radius, and select the neighboring point closest to this radius.你已经拥有了所有元素 ^^
你想要的是距离当前点越远,它的概率就越小,因此你想要使用一个公式,其中距离会降低概率,例如:
d 是当前点与另一个点之间的距离。
所以你要做的就是计算每个点的概率 1/d 并将所有这些概率相加以获得你的总概率或世界概率。
所以类似:
然后你只需要做
并将其与你的点列表进行比较^^;
贾森
you alraedy have all the elements ^^
what you want is that the further away from the current point the less probability it has therefore you want to use a formula where the distance decreases the probability like:
d being the distance between your current point and another.
so what you do with that is calculate for each point their probability 1/d and sum up all these probabilities to get you your total or world.
so something like:
and then you need only to do
and compare it to your list of points ^^;
Jason
您需要一些作为距离函数的概率分布,更具体地说,您需要概率分布的 CDF 或逆 CDF 作为 java 函数
一种可能的选择是指数分布,相应的 CDF 将是
1 -Math.exp( distance * r )
其中 r 是一些用于缩放的常数。同样,您可以使用许多不同的函数,但这个函数非常容易编写代码。然后按距离对点进行排序,并
给出你想要的点(从技术上讲,第一个被选取的概率小于或等于 1 的点 - 均匀随机数,你可以证明它给你返回的概率分布从长远来看,逆 CDF。)
You need some probability distribution that is a function of distance, more specifically, you need the CDF or the inverse-CDF of the probability distribution as a java function
One possible choice is the exponential distribution, and the corresponding CDF would be
1-Math.exp( distance * r )
wherer
is some constant for scaling. Again, there are lots of different functions you could use, but this one is really easy to code up.Then sort the points by distance, and
would give you the point you want (technically the first point whose probability to be picked is less than or equal to 1-the uniform random number, which you can prove gives you back the probability distribution of the inverse CDF in the long run.)