Simulating photon transport through matter

My question is about photon transport through matter. I will try to explain my question as simple as possible.

First we assume that only interaction possible here is Compton scattering. Also we assume that after one scattering there will be no extra scattering. Now a photon is traveling through a slab of a material where Compton scattering electron density is uniform. Now in real practical case that photon can get scattered at any point on the path and at any angle. Hence there are basically two level of randomness in this process. First one is from where it will get scattered and second one is at which angle it will get scattered. I believe, underlying distribution for the first process will be uniform and underlying distribution for second random process is Klein-Nishina differential cross section.Energy of the photon is 511 keV. On that energy range we know that Klein-Nishina probability function will be valid. This probability function will give us probability of scattering with respect to scattering angle.

Now my question is::

How to simulate this practical environment in code. I heard about "Monte Carlo simulation" but I really do not know where to start with. Please give me some suggestion.

Here is a sketch of the algorithm:

1. Generate a uniform random number $y$ between $0$ and $L$ to define the position at which the scattering happens. $L$ in this case is the length of the slab

2. You know that the differential cross section $d\sigma/d\Omega$ is proportional to the probability of finding the scattered particle within a given solid angle. In this case $\sigma$ only depends on $x = \cos\theta$,

$$\frac{d\sigma}{dx} \sim P_E(x) [ P_E(x) + P_E(x)^{-1} - 1 + x^2] = f_E(x)$$

with

$$\frac{1}{P_E(x)} = 1 + \frac{E}{511\; {\rm keV}}(1 - x)$$

3. $f_E(x)$ is normalizable

$$\int_{-1}^{1}dx\; f_E(x) = 1$$

find the norm for the energy scale you want to work with.

4. Once you know $f_E(x)$ there are many ways to draw numbers from it. You could calculate the CDF and invert it (all this is easy to do numerically ---please let me know if you have questions with that---)

Generate a random sample from this to get $x$ and then calculate $\theta$ (remember you defined $x = \cos \theta$)

5. Generate a uniform random number $\phi$ between $0$ and $2\pi$

That is it!

From step (1) you find the position where the photon was scattered

From steps (4) & (5) you get the scattering angle $(\theta,\phi)$