Using Mathematica what is an efficient way to generate a list of $n$ random two dimensional points $\{x_i,y_i\}$ where $i=1,...,n$ so that no two points $p_1$ and $p_2$ in the list has an Euclidean distance lower than say $d$ meaning $\|p_1-p_2\|\leq d$. I came up with the following solution. Though it works I wanted to know if some better method exists. Code
NodeGenetrator[LowerBound_, UpperBound_, DistanceBound_,SampleLength_] :=
Block[{list},
list = RandomReal[{LowerBound, UpperBound}, {1, 2}];
For[i = 0, Length@list <= SampleLength - 1, i++,
list = Module[{NewVal, dist},
NewVal = RandomReal[{LowerBound, UpperBound}, 2];
(*NewVal=RandomVariate[NormalDistribution[
Mean@{LowerBound,UpperBound},DistanceBound],2];*)
dist = Map[EuclideanDistance[NewVal, #] &, list];
If[Min[dist] >= DistanceBound,
AppendTo[list, NewVal],
list
]
];
];
list
];
(* Define function parameters *)
LowerBound = 0;
UpperBound = 100;
DistanceBound = 5;
SampleLength = 60;
sample = NodeGenetrator[LowerBound, UpperBound, DistanceBound,SampleLength];
(ListPlot[#, Frame -> True, Axes -> False,PlotStyle -> PointSize[Large],
AspectRatio -> 1] &)@sample
Output
- As one can see this code uses
For
loop and also keeps on iterating until number of points requested is not met. This somehow makes the execution time for this function unpredictable! - As expected the parameter
DistanceBound
has a important effect on the function behavior. If we tryDistanceBound=12.5
function evaluation becomes very time consuming. - Here we check only one mutual distance condition but is it possible to use a more general test function that checks for more than one mutual characteristics involving those requested number of points that are to be generated.
BR
.. so that NO two points ... has a Euclidean distance lower than say d
in the second line? $\endgroup$