<s>[INST] expected value and variance of the difference of number of people in a row. I need to calculate the expected value and the variance of the following variable:
$n$ people sit in a row, among them person 'a' and person 'b'.
Define $X$ to be the amount of people between 'a' and 'b'. Calculate $E(X)$ and $Var(X)$.  
I have tried to calculate $P(X=k)$, and all I managed to find is $P(X=0)= \frac{n}{2}$ which I dont know if is true. And besides that, I guess that calculating the expectation and the variance directly will be easier than finding the whole distribution, but I simply don't know how to do that.
 [/INST] $P\left(X=k\right) = \frac{\mbox{#positions of A and B with $k$ seats between them}}{\mbox{#total positions for A and B}}$.
When A is to the left of B, there are $n-k-1$ possible positions they can have with $k$ seats between them. When A is to the right of B, this is a mirror image so there is a further $n-k-1$ positions. Total such positions is then $2\left(n-k-1\right)$.
Total number of positions for A and B is $n\left(n-1\right)$ because there are $n$ choices for A's seat and then $n-1$ choices for B's seat.
So, $P\left(X=k\right) = \frac{2\left(n-k-1\right)}{n\left(n-1\right)}$.
Then,
\begin{eqnarray*}
E\left[X\right] &=& \sum_{k=0}^{n-2}{kP\left(X=k\right)} \\
&& \\
&=& \sum_{k=0}^{n-2}{\frac{2k\left(n-k-1\right)}{n\left(n-1\right)}} \\
&& \\
&=& \frac{2}{n\left(n-1\right)} \left[\left(n-1\right) \sum_{k=0}^{n-2}{k} - \sum_{k=0}^{n-2}{k^2} \right] \\
&& \\
&=& \frac{2}{n\left(n-1\right)} \left[\left(n-1\right) \frac{\left(n-2\right)\left(n-1\right)}{2} - \frac{\left(n-2\right)\left(n-1\right)\left(2n-3\right)}{6} \right] \\
&& \\
&=& \frac{1}{3n} \left[3\left(n-2\right)\left(n-1\right) - \left(n-2\right)\left(2n-3\right) \right] \\
&& \\
&=& \frac{1}{3n} \left[3n^2 - 9n + 6 - \left(2n^2 - 7n + 6\right) \right] \\
&& \\
&=& \frac{1}{3n} \left[n^2 - 2n \right] \\
&& \\
&=& \frac{n-2}{3}.
\end{eqnarray*}
And
\begin{eqnarray*}
E\left[X^2\right] &=& \sum_{k=0}^{n-2}{k^2 P\left(X=k\right)} \\
&& \\
&=& \sum_{k=0}^{n-2}{\frac{2k^2 \left(n-k-1\right)}{n\left(n-1\right)}} \\
&& \\
&=& \frac{2}{n\left(n-1\right)} \left[\left(n-1\right) \sum_{k=0}^{n-2}{k^2} - \sum_{k=0}^{n-2}{k^3} \right] \\
&& \\
&=& \frac{2}{n\left(n-1\right)} \left[\left(n-1\right) \frac{\left(n-2\right)\left(n-1\right)\left(2n-3\right)}{6} - \frac{\left(n-2\right)^2\left(n-1\right)^2}{4} \right] \\
&& \\
&=& \frac{\left(n-2\right)\left(n-1\right)}{6n} \left[2\left(2n-3\right) - 3\left(n-2\right) \right] \\
&& \\
&=& \frac{\left(n-2\right)\left(n-1\right)}{6}.
\end{eqnarray*}
Therefore,
\begin{eqnarray*}
Var\left(X\right) &=& E\left[X^2\right] - E\left[X\right]^2 \\
&& \\
&=& \frac{\left(n-2\right)\left(n-1\right)}{6} - \frac{\left(n-2\right)^2}{9} \\
&& \\
&=& \frac{n-2}{18} \left(3n - 3 - \left(2n - 4\right) \right) \\
&& \\
&=& \frac{\left(n-2\right) \left(n+1\right)}{18}.
\end{eqnarray*}
Identities used to evaluate the sums above.
</s>