# How do you uniformly sample spans from a bounded line?

Suppose you have a bounded and continuous line. For example, the line could include all real numbers between 0 and 3. How do you sample spans from the line such that...

• Any point on the line has an equal chance of being included in the span.
• The distribution of span lengths is uniform.

My attempted solution:

import random

max_length = 15.0
bounds = (0.0, 3.0)

length = random.uniform(0, max_length)
start = random.uniform(bounds[0] - length, bounds[1])
end = min(start + length, bounds[1])
start = max(start, bounds[0])

This solution is able to fulfill the first criterion and not the second criterion.

EDIT: Following the lead of BruceET, I have plotted the distribution of span lengths:

import random
from matplotlib import pyplot
import seaborn

max_length = 6
bounds = (0, 3)
num_samples = 10**6

samples = []
for _ in range(num_samples):
length = random.uniform(0, max_length)
start = random.uniform(bounds[0] - length, bounds[1])
end = min(start + length, bounds[1])
start = max(start, bounds[0])
samples.append(end - start)

seaborn.distplot(
samples,
hist=True,
kde=True,
bins=30,
color='darkblue',
hist_kws={'edgecolor': 'black'},
kde_kws={'linewidth': 1})
pyplot.show()

• There are exactly two ways of doing this: (1) make the "spans" all have zero length (and distribute their endpoints uniformly); (2) make them all have length 3. If by "uniform" you allow discrete uniform distributions, there are other solutions. For instance, draw the intervals $[0,3/2)$ and $[3/2, 3)$ with equal probabilities of $1/4$ and draw the intervals $[0,3/4),$ $[3/4,3/2),$ $[3/2,9/4),$ and $[9/4,3)$ with equal probabilities of $1/8.$ The distribution of span lengths is uniform on the set $\{3/4,3/2\}.$ Is this the sort of thing you are looking for? – whuber Aug 23 '20 at 19:31
• Thank you for your creative approach! You are right and that would work. It wouldn't be ideal for me. I should clarify that I'd also like to maximize the variety. For context, this solution will be used to train a machine learning model and diversity is important. – Michael Petrochuk Aug 24 '20 at 0:02
• Generalize, then. For instance, let $n\ge 2$ be a positive integer. Consider the sequence $$(a) = (a_1,a_2,\ldots,a_n)=\frac{1}{n!},\frac{2}{n!},\ldots,\frac{n}{n!}.$$ Each of the $a_i$ is the reciprocal of the integer $n!/i.$ Thus, you may sample $i$ uniformly from the set $\{1,2,\ldots,n\}$ and then, conditional on $i,$ sample a span $[(k-1)a_i, ka_i)$ uniformly from $k\in\{1,2,\ldots, n!/n\}.$ – whuber Aug 24 '20 at 13:37

Comment: You're having trouble with your second simulation because the 'span' of several uniform random variables is is not uniform.

In R, the function range gives the endpoints, taking the difference gives what you call the 'span'.

Here is a simulation in R of the span lengths from 100,000 samples of size $$n=5$$ from $$\mathsf{Unif}(0, 3).$$

set.seed(2020)
span.5 = replicate(10^6, diff(range(runif(5,0,3))))
summary(span.5)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
0.07294 1.63716 2.05782 1.99968 2.41826 2.99932
hist(span.5, prob=T, col="skyblue2")
curve((1/3)*dbeta(x/3, 4, 2), add=T, col="red", lwd=2)

• Yes. Your right. Since the above problem allows me to choose the length, I have been thinking about picking a length to correct for that. – Michael Petrochuk Aug 23 '20 at 18:45
• In case it helps, I just now included the density function of the theoretical 4-parameter beta distribution. – BruceET Aug 23 '20 at 18:51
• Thanks for your comment and that's helpful. I updated the question with the current distribution that I am seeing. – Michael Petrochuk Aug 24 '20 at 4:08
• Do you know the name of the distribution that I posted above? I think if I can find the inverse of that distribution then I can correct for it. – Michael Petrochuk Aug 24 '20 at 4:15
• @MichaelPetrochuk if you enforce the lengths to be uniform by IRT or such, the sampling probability along the line will no longer be uniform. Your criteria are more fundamentally incompatible: any points near the center of the line are "easier" to sample because of overlaps (e.g. think of all possible placements for segments of length $2$), so unless you prevent overlaps or enforce some kind of correlation between segment position and length, which you probably don't want, I don't see how this is possible. Sampling along a circle, on the other hand, would be easy, if that helps... – juod Aug 24 '20 at 6:40