Random selection of points in the plane, with a higher probability of selection specified closer to the points

I have a problem, I'm not too sure how to solve it. I have 2d space with several points in it. I also have a current point, which is one of the points in space. I want to randomly select one of the other points, with a higher probability of selection being given to points closer to my current point. I am working in Java. Any advice would be highly appreciated.

+3
source share
3 answers
  • Assign a "weight" to each point, for example, by calculating 1 / distanceFromCurrent.

  • Select a point based on these scales.

The solution for the last part can, for example, be found in some of the following answers:


- java.util.Random.nextGaussian. , , , .

+4

^^

, , , , , , , :

1/

d - .

, 1/d , .

- :

total = 0;
for(MyPoint p : list){
   p.probability = 1/(distance(currentpoint,p);
   total += p.probability;
}

Math.random*total;

^^;

+1

, , , CDF CDF java-

/**
 * @param distance
 * @return probability of choosing a point closer than distance
**/
double someCDF( double distance );

, CDF 1-Math.exp( distance * r ), r - . , , , .

double rnum = Math.random();
for( Point point : sortedPoints )
    if( someCDF( distance(thisPoint,point) ) >= rnum )
        return point;

, ( , , 1 - , , CDF ).

0
source

All Articles