1. 3
  1.  

  2. 2

    With simple fix you can make it simpler and more accurate:

    def monte_pi(n):                   
        points_in_circle = 0
        points_in_square = uniform(0.0, 1.0, (n, 2))
        for x, y in points_in_square:
            if (x**2 + y**2 < 1):   
                points_in_circle += 1
        circle_area = points_in_circle / len(points_in_square)
        return circle_area * 4
    

    It simply calculates area in the first quadrant and then multiply result by 4. Additionally it uses unitary circle to minimise the rounding errors.

    1. 1

      Hey! Thanks for sharing. That was actually my original solution, but I realized some non programmer people might be a little confused looking at the code if it didn’t perfectly represent what I explained. Nice with the errors. I certainly didn’t know the most precise approach to use for this!

    Stories with similar links:

    1. Enterprise C# to Self Employed React and Blazor - followup authored by jee 1 year ago | 2 points | no comments
    2. 1 year on Hacker News authored by jee 2 years ago | -2 points | 4 comments
    3. Migrating from Enterprise C# to Self Employed Clojure & React authored by jee 2 years ago | 3 points | no comments
    4. Why Functional Programming authored by jee 2 years ago | 10 points | 7 comments