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)

    hist_kws={'edgecolor': 'black'},
    kde_kws={'linewidth': 1})

Distribution of span lengths

  • $\begingroup$ 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? $\endgroup$
    – whuber
    Aug 23, 2020 at 19:31
  • $\begingroup$ 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. $\endgroup$ Aug 24, 2020 at 0:02
  • $\begingroup$ 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\}.$ $\endgroup$
    – whuber
    Aug 24, 2020 at 13:37

1 Answer 1


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).$

span.5 = replicate(10^6, diff(range(runif(5,0,3))))
   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)

enter image description here

  • $\begingroup$ 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. $\endgroup$ Aug 23, 2020 at 18:45
  • $\begingroup$ In case it helps, I just now included the density function of the theoretical 4-parameter beta distribution. $\endgroup$
    – BruceET
    Aug 23, 2020 at 18:51
  • $\begingroup$ Thanks for your comment and that's helpful. I updated the question with the current distribution that I am seeing. $\endgroup$ Aug 24, 2020 at 4:08
  • $\begingroup$ 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. $\endgroup$ Aug 24, 2020 at 4:15
  • 2
    $\begingroup$ @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... $\endgroup$
    – juod
    Aug 24, 2020 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.