Thread: Math Quiz 9
View Single Post
  #12   Spotlight this post!  
Unread 21-07-2016, 11:36
z_beeblebrox's Avatar
z_beeblebrox z_beeblebrox is offline
Custom User Title
AKA: Cal
FRC #4183 (Bit Buckets)
Team Role: Alumni
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Cambridge MA
Posts: 811
z_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond reputez_beeblebrox has a reputation beyond repute
Re: Math Quiz 9

Quote:
Originally Posted by Ether View Post


What's the average length of all the line segments whose endpoints both lie on the unit square's perimeter?


Modified above Monte Carlo code to solve this:

Code:
import numpy as np

iterations = 100000

avg = 0

for i in range(1000):
    print(i)
    for j in range(iterations):
        perimeter = 4*np.random.rand(2)

        if perimeter[0] < 1:
            y1 = 0
            x1 = perimeter[0]
        elif perimeter [0] < 2:
            y1 = perimeter[0] - 1
            x1 = 1
        elif perimeter[0] < 3:
            y1 = 1
            x1 = 3 - perimeter[0]
        else:
            y1 = 4 - perimeter[0]
            x1 = 0

        if perimeter[1] < 1:
            y2 = 0
            x2 = perimeter[1]
        elif perimeter[1] < 2:
            y2 = perimeter[1] - 1
            x2 = 1
        elif perimeter[1] < 3:
            y2 = 1
            x2 = 3 - perimeter[1]
        else:
            y2 = 4 - perimeter[1]
            x2 = 0

        length = np.sqrt((x1-x2)**2+(y1-y2)**2)
        avg = (avg*j + length)/(j+1)
    with open("9.1.txt", "a") as f:
        f.write(str(avg)+'\n')
2.1e7 iterations yields 0.73511, standard deviation 0.00095 (between blocks averaging 1e6 iterations). More iterations will give a more accurate answer.
__________________
2012 Utah Regional Rookie All-Star
2013 Phoenix Regional Judge's Award for "design process and prototyping"
2014 Hub City Regional Quality Award, Arizona Regional Excellence in Engineering Award
2015 Arizona East Regional Creativity Award, Winner
2016 Arizona North Regional Finalist, Arizona West Excellence in Engineering Award, Finalist

Last edited by z_beeblebrox : 21-07-2016 at 11:44.
Reply With Quote