Jun-30-2018, 06:10 PM
(This post was last modified: Jun-30-2018, 06:10 PM by supersingh.)
Hey guys,
I am trying to improve upon a Buffon Needle Simulation. Basically, what I need is code that would require me to select an amount of needles to 'drop' (X) and give me the value of 'hits' on the surface lines, and repeat the process N times (trials) and since each trial gives different values of hits, I want it to generate the mean hit value automatically from the N trials. So far, I have managed to loop the code in order to provide me with the different number of hits based upon the number of trials. But now I am confused on how to automatically generate the mean of the values shown. I tried using an average function but it didn't work. The code is as follows:
I am trying to improve upon a Buffon Needle Simulation. Basically, what I need is code that would require me to select an amount of needles to 'drop' (X) and give me the value of 'hits' on the surface lines, and repeat the process N times (trials) and since each trial gives different values of hits, I want it to generate the mean hit value automatically from the N trials. So far, I have managed to loop the code in order to provide me with the different number of hits based upon the number of trials. But now I am confused on how to automatically generate the mean of the values shown. I tried using an average function but it didn't work. The code is as follows:
print "Hit list" for hit in range(3): #number of trials import math import random hit = 0 total = 0 deg = random.randint(0,180) pos_mid = random.randint(2000,8000) / 1000 pos_bot = pos_mid - (math.sin(math.radians(deg)) / 2) pos_top = pos_mid + (math.sin(math.radians(deg)) / 2) acc = 100 #number of needles thrown while total < acc: if pos_bot % 2 == 0 or pos_top % 2 == 0: hit += 1 elif pos_bot < 2 and pos_top > 2: hit += 1 elif pos_bot < 4 and pos_top > 4: hit += 1 elif pos_bot < 6 and pos_top > 6: hit += 1 elif pos_bot < 8 and pos_top > 8: hit += 1 else : hit = hit total += 1 deg = random.randint(0,180) pos_mid = random.randint(2000,8000) / 1000 pos_bot = pos_mid - (math.sin(math.radians(deg)) / 2) pos_top = pos_mid + (math.sin(math.radians(deg)) / 2) if hit == 0: print ("0") else: print (hit) #needles that intersect the lines print "Average" x = [hit] print (x) avg = sum (x) / len(x) print (avg)Help would be greatly appreciated