import numpy as np import numpy.random import matplotlib.pyplot as plt N = 100 m = 100000 Nruns = 10 # Question 1 def aleatoire(N, m): base = np.zeros((N,N)) w = [] for i in range(m): x = numpy.random.randint(0,N) y = numpy.random.randint(0,N) base[x][y] += 1 w.append(np.max(base)-np.min(base)) return (base, w) base, w = aleatoire(N, m) # Question 2 plt.imshow(base) plt.colorbar() plt.savefig('aleatoire.png') plt.close() # Question 3 wtota = [] for i in range(Nruns): print "Run...", i base, w = aleatoire(N, m) wtota.append(w) wtota = np.array(wtota) wavga = np.mean(wtota,0) wstda = np.std(wtota, 0) plt.errorbar(range(len(wavga)), wavga, wstda) plt.savefig('w-aleatoire.png') plt.close() # Question 4 def ballistic(N, m): base = np.zeros((N,N)) w = [] for i in range(m): x = numpy.random.randint(0,N) y = numpy.random.randint(0,N) north = (y+1)%N south = (y-1)%N east = (x+1)%N west = (x-1)%N height = np.max([base[x][y] + 1, base[east][y], base[west][y], base[x][north], base[x][south]]) base[x][y] = height w.append(np.max(base)-np.min(base)) return (base, w) wtotb = [] for i in range(Nruns): print "Run...", i base, w = ballistic(N, m) wtotb.append(w) plt.imshow(base) plt.colorbar() plt.savefig('ballistic.png') plt.close() wtotb = np.array(wtotb) wavgb = np.mean(wtotb,0) wstdb = np.std(wtotb, 0) plt.errorbar(range(len(wavgb)), wavgb, wstdb) plt.savefig('w-ballistic.png') plt.close() # Question 5 plt.errorbar(range(len(wavga)), wavga, wstda) plt.errorbar(range(len(wavgb)), wavgb, wstdb) plt.savefig('w-comparison.png') plt.close()