Solution (source code)

= Solution

The <probability generating function of an independent random sum> states that, for a nonnegative integer-valued $N$ independent of <independent and identically distributed> nonnegative integer-valued summands, $G_{\sum_{i=1}^NX_i}(s)=G_N(G_{X_1}(s))$ wherever the expectations converge. Here $G_{X_1}(s)=1-r+rs$, so, writing $q=1-p$,
$$
\boxed{G_Z(s)=\frac{p}{1-q(1-r+rs)}=\frac{p}{p+qr-qrs}.}
$$
It is valid at least for $|s|\le1$, and its power series in fact converges for $|s|<(p+qr)/(qr)$.