12. Ball (from 8th grade. 3 points). At the ball, $n$ family couples arrived. In each couple, the husband and wife are of exactly the same height, but there are no two couples of the same height. A waltz begins, and all the attendees randomly form pairs: each gentleman dances with a randomly chosen lady. Find the mathematical expectation of the random variable $X$ "The number of gentlemen who are shorter than their partner."
Solution. Let's assign the cavaliers numbers from 1 to $n$. Let $I_{j}$ be the indicator of the event "cavalier number $j$ is shorter than his lady."

$$
\mathrm{P}\left(I_{j}=1\right)=\frac{j-1}{n} . \text { Then } \mathrm{E} X=\mathrm{E} I_{1}+\mathrm{E} I_{2}+\ldots+\mathrm{E} I_{n}=\frac{1}{n}(0+1+\ldots+n-1)=\frac{n(n-1)}{2 n}=\frac{n-1}{2}
$$

Answer: $\mathrm{E} X=\frac{n-1}{2}$.