Results 1 to 7 of 7

Thread: Using Monte Carlo simulation to estimate Pi

  1. #1
    Join Date
    Dec 2011
    Posts
    142
    Mentioned
    0 Post(s)
    Quoted
    12 Post(s)

    Default Using Monte Carlo simulation to estimate Pi

    I'm in a intro to Python class and our assignment is to estimate Pi but I keep getting an error I don't understand. The error is
    Code:
    NameError: name 'win' is not defined
    The reason I don't understand the error is that I've defined win in the
    Code:
    def create_win(w,h)
    function.
    My code is:

    Code:
    from graphics import *
    from math import *
    import random
    
    def generateXY(n):
        xlist = []
        ylist = []
        for i in range(n):
            xlist.append(random.uniform(-1,1))
            ylist.append(random.uniform(-1,1))
        return xlist, ylist
    
    
    def create_window(w, h):
        win = GraphWin("Monte Carlo",w,h)
        win.setCoords(-1,-1,1,1)
        return win
    
    # create points list
    def create_points(xlist, ylist):
        points = []
        for i in range(len(xlist)):
            points.append((xlist[i], ylist[i]))
        return points
        
    # draw a circle with given radius
    def draw_circle(w, radius):
        center = Point(0,0)
        c= Circle(center, radius)
        c.draw(win)
        return c
    def draw_points(win, xylist):
        for point in xylist:
            p = Point (*point)
            p.draw(win)
    
    def distance_from_center(x,y):
        return math.sqrt((x ** 2 + y ** 2))
    
    # estimate pi using Monte Carlo simulation
    def simulate_pi(win, xylist):
        draw_points(win, xylist)
        total_points = len(xylist)
        points_inside = 0
    
        for p in xylist:
            if (distance_from_center(p[0],p[1]) <= 1):
                points_inside += 1
        percentError = points_inside/total_points
        if percentError == .85:
            return (8.22536130248883)
        estimate_pi = 4 * (points_inside/total_points)
        print(estimate_pi, percentError)
                
        return percentError
        
                    
    def main():
        xlist,ylist = generateXY(1000)
        xylist = create_points(xlist, ylist)
        win = create_window(500,500)
        draw_circle(win, 1)
        error = simulate_pi(win, xylist)
        win.getMouse()
        win.close
    if __name__ == '__main__':
        main()
    Any help is much appreciated.

    Thanks a bunch,
    ~I Got This

  2. #2
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    733
    Mentioned
    81 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by I got this View Post
    I Got This
    Oh okay nvm then

  3. #3
    Join Date
    Dec 2011
    Posts
    142
    Mentioned
    0 Post(s)
    Quoted
    12 Post(s)

    Default

    Quote Originally Posted by Ross View Post
    Oh okay nvm then
    Yeah my name is clearly inaccurate considering all my posts are asking for help <_<

  4. #4
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    733
    Mentioned
    81 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by I got this View Post
    Yeah my name is clearly inaccurate considering all my posts are asking for help <_<
    I'm just messin' man. Good luck!

  5. #5
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by I got this View Post
    I'm in a intro to Python class and our assignment is to estimate Pi but I keep getting an error I don't understand. The error is
    Code:
    NameError: name 'win' is not defined
    The reason I don't understand the error is that I've defined win in the
    Code:
    def create_win(w,h)
    function.


    Python Code:
    def draw_circle(w, radius):
        center = Point(0,0)
        c = Circle(center, radius)
        c.draw(win)
        return c

    Where did you define win? Is w supposed to be win? In this function, win is undefined and w is never used.
    Last edited by Brandon; 02-06-2016 at 02:43 AM.
    I am Ggzz..
    Hackintosher

  6. #6
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

    Default

    this thread wouldnt be complete without a version in simba...
    Simba Code:
    program pi;
    const
      n = $FFFFF;
    var
      i: integer;
    begin
    i := 0;
    for 1 to n do
      if (random()**2 + random()**2) <= 1 then
        i := i + 1;
    writeln('pi is approx ', i*4.0/n);
    end.

    Code:
    def simulate_pi(win, xylist):
        draw_points(win, xylist)
        total_points = len(xylist)
        points_inside = 0
    
        for p in xylist:
            if (distance_from_center(p[0],p[1]) <= 1):
                points_inside += 1
        percentError = points_inside/total_points
        if percentError == .85:
            return (8.22536130248883)
        estimate_pi = 4 * (points_inside/total_points)
        print(estimate_pi, percentError)
                
        return percentError
    So... im confused. why is percent error this? shouldnt the percent error be (estimate - pi)/pi ?
    where did 8.22536130248883 come from?

  7. #7
    Join Date
    Jan 2013
    Posts
    146
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default

    I'd say brandon is right.

    Does it say what line the error occurred on?

    Im Back... Previously known as Megaleech
    [Herbalife]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •