What is the density function of a distribution that has another random variable in itself? I am working on a problem in which we know that a certain population follows a uniform distribution over $[M-(1/2),M+(1/2)]$, and we know that $M$ follows a uniform distribution over $[0,1]$. So we have a kind of uniform distribution inside another. We know that the density function for $M$ is $1$, but how do we get the density function for the population, since that should depend on $M$?
 A: Simulating the process in R statistical software to support @mzp's analytic Answer (+1).
B = 10^5; x = numeric(B)
for (i in 1:B) {
  m = runif(1);  x[i] = runif(1, m-.5, m+.5) }
hist(x, prob=T, br=25, col="skyblue2")


A: Let $X\sim U(M-0.5,M+0.5)$ where $M\sim U(0,1)$. From the formula for conditional probability density function we have that
$$f_{X,M}(x,m) = f_X(x|M=m)f_M(m).$$
Since
$$f_M(m) = \begin{cases}1,\quad m\in[0,1]\\0,\quad \text{otherwise}\end{cases}$$
and
$$f_X(x|M=m) = \begin{cases}1,\quad x\in[m-0.5,m+0.5]\\0,\quad \text{otherwise}\end{cases}$$
it follows that
$$f_{X,M}(x,m) = \begin{cases}1,\quad m\in[\max(0,x-0.5),\min(1,x+0.5)]\\0,\quad \text{otherwise}\end{cases}$$
Finally, for $x\in[-0.5,1.5]$,
$$f_{X}(x) = \int_{-\infty}^{\infty} f_{X,M}(x,m)dm=\int_{\max(0,x-0.5)}^{\min(1,x+0.5)} 1dm=\min(1,x+0.5)-\max(0,x-0.5),$$
or
$$ f_{X}(x) = 
\begin{cases}
0\quad\quad\;\;,\quad x<-0.5\\
0.5+x,\quad x\in[-0.5,0.5]\\
1.5-x,\quad x\in(0.5,1.5]\\
0\quad\quad\;\;,\quad x>1.5\\
\end{cases}$$
