# Sampling uniformly from all possible line segments of a given length that fit inside a container

Consider that task of randomly placing a line segment of some length $L$ near a plane s.t. a point $p$ at the center of the line segment is at most a distance $H$ from the plane and intersections between the line segment and the plane are forbidden. Alternatively, consider the task of randomly placing the line segment in a box or sphere that is large enough to contain the line segment without intersection between the segment and the boundaries of the container.

What is a proper procedure for "randomly" placing the line segment in scenarios like those outlined above s.t. a placement operation constitutes a uniform random sampling of all legal configurations of the line segment? For example, it's not at all clear to me that procedures such as: (a) selecting two points a distance $L$ apart and testing for intersection, or (b) selecting one point for the center of the line segment that randomly generating $(r,\theta,\phi)$ values for the line segment's 3D orientation, then testing for intersection, are free from bias.

Does it depend on the exact container geometry, or is there a general solution? What about for cylinders of radius $r$?

Maybe to talk a bit more about what I'm after - if we had something like a monoatomic gas molecule diffusing in a box via a Brownian motion, we can say (to rough approximation) that the system is ergodic and that a uniformly chosen coordinate, with room to admit a sphere approximately as large as the atomic particle, represents a uniform random sampling of a possible position of the particle. Of course, we also have to assume some kind of "reachability" condition here, i.e. that the particle can diffuse from one point to any other point. How can we perform the same random sampling if we stretched the sphere out into a cylinder?

As an example, consider a unit-length segment $s$ that must lie in the upper halfplane of $\mathbb{R}^2$ (your opening example). Let $\theta$ be the angle of $s$ w.r.t. the vertical. Use the midpoint $m$ of $s$ to indicate its position. For $\theta=0$, $m$ must lie in $y \ge \frac{1}{2}$. For other values of $\theta$, $m$ lies in $y \ge \frac{1}{2} \cos \theta$. Imagine stacking up all these half-space regions as $\theta$ increases. Now you have a $3$-dimensional configuration space for all legal placements of $s$.