#!/usr/bin/env python
'''
montecarlo.py
Eric Ayars
June 2016
Python solutions to PICUP "Monte Carlo error propagation"
exercise set.
'''
from pylab import *
import random
#########################
# Exercise 1
#########################
# Generate the gaussian distribution
N = 1000 # How many to generate
mu = 5.4 # mean
sigma = 0.2 # sigma
dist = array([random.gauss(mu, sigma) for j in range(N)])
# Plot a histogram of the distribution
hist(dist, 30)
show()
#########################
# Exercise 3
#########################
# characteristits of d and t
avg_d = 5.4
sigma_d = 0.2
avg_t = 6.2
sigma_t = 1.5
N = 1000 # How many to generate
# Generate distribution of distances
d = array([random.gauss(avg_d, sigma_d) for j in range(N)])
t = array([random.gauss(avg_t, sigma_t) for j in range(N)])
# Since d and t are numpy arrays, we can just divide, the
# resulting index-wise multiplication will generate another
# numpy array of speeds.
v = d/t
# plot histogram of velocity
hist(v, 30)
show()
#########################
# Exercise 4
#########################
# report average and sigma
print('mean speed = %0.3f' % mean(v))
print('median speed = %0.3f' % median(v))
print('standard deviation = %0.3f' % std(v))