def test_seed(self): #unseeded r0 = random.uniform(0, 1) r1 = random.expovariate(3000) r2 = random.gauss(1, 3) #seed random.seed(0xdeadbeef) a0 = random.uniform(0, 1) a1 = random.expovariate(3) a2 = random.gauss(1, 3) #reseed random.seed(0xdeadbeef) b0 = random.uniform(0, 1) b1 = random.expovariate(3) b2 = random.gauss(1, 3) #unseeded should be different to seeded self.assertFalse(a0 == r0) self.assertFalse(a1 == r1) self.assertFalse(a2 == r2) #reseeded should be same as seeded self.assertEqual(a0, b0) self.assertEqual(a1, b1) self.assertEqual(a2, b2)
def path_length(self, ptc): '''path before decay within material, for a particle. The particle must have a method is_em to indicate if the radiation or interaction length should be used. ''' freepath = self.x0 if ptc.is_em() else self.lambdaI if freepath == 0.0: return sys.float_info.max else: return random.expovariate(1./freepath)
'''Describe material used in a detector layer.''' def __init__(self, name, x0, lambdaI): '''Parameters: - name: e.g. iron - x0: radiation length - lambdaI : interaction length ''' self.name = name self.x0 = x0 self.lambdaI = lambdaI def path_length(self, ptc): '''path before decay within material, for a particle. The particle must have a method is_em to indicate if the radiation or interaction length should be used. ''' freepath = self.x0 if ptc.is_em() else self.lambdaI if freepath == 0.0: return sys.float_info.max else: return random.expovariate(1./freepath) void = Material('void', 0, 0) if __name__ == '__main__': import matplotlib.pyplot as plt a = [random.expovariate(25.) for i in range(10000)] n, bins, patches = plt.hist(a, 50, normed=1, facecolor='green', alpha=0.75) plt.show()