Solution (source code)

= Solution

Let $I_i$ indicate that student $i$ stays awake. These indicators have independent <Bernoulli distributions> with parameter $p$, and $W=\sum_{i=1}^M I_i$. The <Poisson distribution> has <probability generating function>
$$
G_M(u)=\sum_{m=0}^{\infty}e^{-\lambda}\frac{\lambda^m}{m!}u^m
=e^{\lambda(u-1)}.
$$
The <probability generating function of an independent random sum> now gives
$$
G_W(s)=G_M(1-p+ps)=e^{\lambda p(s-1)}.
$$
Recognizing the <Poisson distribution>,
$$
\boxed{W\sim\operatorname{Poisson}(\lambda p),\qquad
\mathbb P(W=w)=e^{-\lambda p}\frac{(\lambda p)^w}{w!},\quad w\geq0.}
$$
This is <Poisson thinning>.