Solution (source code)

= Solution
{c}

For $0<r<1$, start at any $x\in\{0,\ldots,n\}$. Propose $Y$ uniformly from this same finite set and accept it with probability
$$
\boxed{\min\left\{1,\frac{\binom nY}{\binom nx}\left(\frac r{1-r}\right)^{Y-x}\right\}.}
$$
Otherwise retain $x$. The uniform proposal is symmetric, so this is the <Metropolis–Hastings algorithm> with target $\pi(\cdot\mid r)$. Detailed balance follows because accepted probability flow is proportional to $\min\{\pi(x),\pi(Y)\}$. The chain is irreducible and has positive self-transition probability, hence converges to that binomial equilibrium law. At the degenerate parameters zero or one, the target is simply a point mass and can be simulated as a constant chain.