# Create n random points on [0,1] with minimum distance

Given a positive integer n and a small positive real number s.

How to create distribution for n real numbers p1, p2, p3, ..., pn such that

0 ≤ p1 < p2 < p3 <... < pn ≤ 1
and
p1, p2-p1, p3-p2, ... 1-pn ≥ s


?

• Do you want a symbolic distribution or a way to generate samples that satisfy your restrictions?
– JimB
Mar 7 at 3:41
• I's not exactly a solution to your problem, but related is HardcorePointProcess like this: points = Sort@Flatten@RandomPointConfiguration[HardcorePointProcess[50.0, 0.01, 1], Line[{{0}, {1}}]]["Points"]; . I don't think this has the boundary contraints you want though, and it also doesn't let you specify an exact number of points. Mar 7 at 10:50
• Hello. I am a lost mathematician who has wandered in from MSE. I don't know anything about Mathematica! However, if I understand your question correctly - "how can I uniformly generate real numbers $p_i$ according to these constraints" - I have something to say. This problem is amenable to a nice elementary trick: consider instead the numbers $q_i = p_i - is$ (and note that this transformation is measure-preserving). The corresponding constraint is simply $0 \le q_1 \le q_2 \le \dots \le q_n \le 1 - (n + 1)s$... Mar 8 at 2:00
• So you can solve this problem without rejection sampling by using your favourite method to uniformly generate sorted numbers in $[0, 1 - (n + 1)s]$ (eg stackoverflow.com/a/66366347 or just generate random numbers & sort them), and then shifting them to get $p_i$. Mar 8 at 2:00

If you want to generate independent random samples under the stated restrictions and $$s<1/(n+1)$$, then consider the following:

n = 10;
s = 1/20;
p = ConstantArray[0, n];
count = 0;
SeedRandom[12345];
While[Min[p] < s || Max[p] > 1 - s || Min[Differences[p]] < s,
count = count + 1;
p = Sort[RandomVariate[UniformDistribution[{s, 1 - s}], n]]]

p
(* {0.16256, 0.296157, 0.368503, 0.446481, 0.510143, 0.56314, 0.675157,  0.726587, 0.836507, 0.93472} *)
s // N
(* 0.05 *)
Min[Differences[p]]
(* 0.0514309 *)
count
(* 359 - Number of samples that had to be rejected *)

• neat, +1 of course :) Mar 7 at 11:14
• Hello Jim. This might be a really silly question, so sorry about that - but I am trying to understand how your code works and I can't work out the purpose of the part Min[p] < s || Max[p] > 1 - s - isn't that (almost always*) guaranteed by sampling from a uniform distribution on $[s, 1 - s]$? Does it have to do with the starting condition of the loop, maybe? *: It also seems that the question only asks for a non-strict inequality between $p_1$ and $s$ and $p_n$ and $1 - s$ anyway. Mar 8 at 2:07
• Not a silly question (and thank you for the diplomatic approach). I needed to include that restriction because I started out with p = ConstantArray[0, n]. But had I started out with p=RandomVariate[UniformDistribution[{s,1-s}], n, I would be able to remove those pieces of code. But certainly you are correct in that given the way I did it, Max[p] > 1 - s isn't necessary at all.
– JimB
Mar 8 at 3:36
• Also, note that this approach is extremely inefficient when $s$ is close to $1/(n+1)$.
– JimB
Mar 8 at 6:18

Thanks to the comment from Izaak van Dongen, it can be solved with straightly :

umd[n_, s_] := Sort[RandomReal[{0, 1 - (n + 1) s}, n]] + s  Range[n];


Pick 0 < p1 < p2 < p3 < p4 < p5 < 1, such that difference of adjacent elements is at least 0.1

Table[ListPlot[umd[5, 1/10], ImageSize -> 50, AspectRatio -> 2,
PlotRangePadding -> {0.5}, PlotStyle -> Red, Filling -> Axis,
FillingStyle -> Red, PlotRange -> {{1, 5}, {0, 1}}], 10] // Row


I wonder if this is better than the previous answers.

• This is far faster than any of the others. Also, the HardcorePointProcess seems to involve a "mystery parameter" that can affect the distribution of any of the ordered points. In other words, there are speed and potential distribution differences among the answers so "better" depends on what you're looking for.
– JimB
Mar 8 at 21:23
• You probably want some error handling and to raise a message if n*s > 1, because in something like umd[11, .1] the first and last elements are out of bounds. Mar 20 at 13:01

Thanks for @Ulrich Neumann's advice for code improvement.

The idea of the below code is to iteratively generate points, ensuring that each new point maintains the minimum distance $$s$$ from all previously generated points.

(*Initialize variables*)n = 10;

(*Number of points*)s = 1/20;

(*Minimum distance*)
points = {};(*List to hold the points*)

(*Function to check if a new point is at least distance s from all existingpoints*)
isValidPoint[newPoint_, points_, s_] :=
AllTrue[points, RealAbs[# - newPoint] >= s &];

(*Generate points*)

While[Length[points] < n, newPoint = RandomReal[{s, 1 - s}];
If[isValidPoint[newPoint, points, s], AppendTo[points, newPoint];];
(*Optional:
include a check to break out if it's not possible to add more points*)];

(*Sort points for better visualization*)
points = Sort[points];

points
Min[Differences[points]] >= s  (*True*)

• @Aspen Nice solution! Change Abs to RealAbs (and newPoint = RandomReal[{s, 1-s}]) which makes code run considerable faster for greater n. Mar 7 at 8:17

The most "don't make me think" approach in order to avoid scratching head regarding uniformity of sampling of such a distribution is to specify it as an $$n$$-dimensional implicit geometric region and sample points from it using RandomPoint. The problem with this approach is that RandomPoint performs some sort of an algebraic decomposition of the system of inequalities and the run time of this is dependent on $$n$$ and $$s$$, in practice increasing exponentially as $$n$$ increases, putting limit of practicality somewhere at $$n \ll 15$$. The good part is that once this decomposition is done, sampling many values is not horribly slow.

In numerical sampling (and when $$s \gt 0$$) you are never going to see $$0$$ or $$1$$ in results anyway, so I simplified $$\le$$ to $$\lt$$ in these edge cases.

samples =
With[{n = 8, s = 1/10},
With[{vals = {0, Sequence @@ Array[p, n], 1}},
RandomPoint[
ImplicitRegion[
Less @@ vals &&
And @@ (# >= s & /@ Differences[vals]),
Evaluate[Array[p, n]]],
25]]];


We can see that values are distributed "evenly":

NumberLinePlot[samples]


Also, differences between consecutive values are at least $$\frac{1}{10}$$:

NumberLinePlot[Differences /@ samples]


With $$25000$$ samples we can also see distributions of each $$p_n$$:

Histogram[Transpose[samples], {0.005}, "PDF"]


Similarly, symbolic CDFs for individual $$p_n$$ can be acquired using Probability and Conditioned:

cdfs =
With[{n = 8, s = 1/10},
With[{vals = {0, Sequence @@ Array[p, n], 1}},
ParallelTable[
Probability[
Conditioned[
p[i] <= x,
Less @@ vals &&
And @@ (# >= s & /@ Differences[vals])],
Distributed[Array[p, n], UniformDistribution[Table[{0, 1}, n]]]],
{i, n}]]];


Symbolic PDFs correspond to those seen on the histogram:

Plot[D[cdfs, x], {x, 1/10, 9/10}, Evaluated -> True]


You can do this by simulating a HardcorePointProcess till you've hit the right number of points:

n = 10; (* number of points *)
r = 0.05; (* exclusion radius *)
reg = Interval[{0, 1}];
mu =  30 (* You'll need to guess an appropriate value for this *)
proc = HardcorePointProcess[mu, r, 1];
ResourceFunction["Until"][
points["PointCount"] === 10
,
points = RandomPointConfiguration[proc, reg]
];
Sort @ Flatten @ points["Points"]
Differences[%] (* check the distances *)


You can make a rough estimate for mu as:

volume = RegionMeasure[reg];
mu =  n/(volume + r - n * r)


You can find this by estimating by assuming that the number of points is roughly equal to the intensity of the process times the effective volume of the region (i.e., the volume without the excluded hard cores):

Clear[mu, volume, n, r]
Solve[mu * (volume - (n - 1)  r) == n, mu]


If the formula give a negative number, you're probably trying to stuff too many points into the interval to fit, but you can still try a really high value of mu to see if you can get a result.

• When I run this with SeedRandom[12345], I get p[1] < 0.05 and p[n] > 1-0.05 although all of the distances between points are at least 0.05. Maybe the OP doesn't really need the restrictions at the extremes.
– JimB
Mar 8 at 4:37
• Oh, I missed the boundary constraints. I guess you can fit those in by just shrinking the region a bit. Mar 8 at 8:31