1

I am writing a code about a mono-energetic gamma beam which the dominated interaction is photoelectric absorption, mu=2 cm-1, and i need to generate 50000 random numbers and sample the interaction depth(which I do not know if i did it or not). I know that the mean free path=mu-1, but I need to find the mean free path from the simulation and from mu and compare them, is what I did right in the code or not?

import random
import matplotlib.pyplot as plt
import numpy as np

mu=(2)
random.seed=()
data = np.random.randn(50000)*10
bins = np.arange(data.min(), data.max()+1e-8, 0.1)
meanfreepath = 1/mu
print(meanfreepath)
plt.hist(data, bins=bins)
plt.show()
khelwood
  • 55,782
  • 14
  • 81
  • 108
James
  • 35
  • 1
  • 5
  • You know your algorithm best. How would you assert that your answers are correct? Hint: you can pick known input and output values across a range and assert your code honours those. This is why even simple programs should have functions that can be fed data from the calling application. –  May 25 '18 at 15:58

1 Answers1

1

Well, interaction depth distribution is Exponential one, not a gaussian.

So code would be

lmbda = 2 # cm^-1
beta  = 1.0/lmbda

data = np.random.exponential(scale=beta, size=50000)
mfp = np.mean(data)
print(mfp)

# build histogram

More details at https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.random.exponential.html

Code above produced

0.4977168417102998

which looks like 2-1 to me

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64