-2

Write a function monteCarloPi(n, radius) that takes two arguments, the number of simulations n and the radius of the circle, and returns a float, which is the estimated value for pi. This radius should be the same radius that you used to draw your inscribed circle in the previous problem. Generate a set of random points, and test to see if that value is inside the circle or outside the circle. Use turtle to draw a dot at each location. This can be done using the function turtle.dot(size, color). Find the ratio of the points inside the circle to the number of points in the simulation. The latter number is an estimate for the area of the square. Multiple the ratio by four to get your estimate for pi.

This is what I have and I have no idea why it only draws one dot. Can anybody help me? I am a beginner /:

import turtle as t
import random

def monteCarloPi(n, radius):
    '''
    Takes two arguments, the number of simulations n and the radius of the circle, and returns a float, which is the estimated value for pi.
    
    '''
    
    t.dot() # Origin (0, 0)
    t.pu()
    t.goto(0, -radius)
    t.pd()
    t.circle(radius)
    t.pu()
    t.goto(-radius ,-radius)
    t.pd()
    
    for square in range(4):
        t.fd(radius * 2) 
        t.lt(90)
        
    points_in_circle = 0
    points_in_square = 0
    x = random.uniform(-radius, radius)
    y = random.uniform(-radius, radius)
    
    for dots in range(n):
        t.pu()
        t.goto(x, y)
        t.dot()
        
        origin = x ** 2 + y ** 2
        if origin <=1 :
            points_in_circle += 1
        else:
            points_in_square +=1
            
        pi = 4 * (points_in_circle / points_in_square)
        return pi
cadolphs
  • 9,014
  • 1
  • 24
  • 41
  • in your description you have few tasks to do and for every task you should create separated function. This way you split problem to simpler problems. `monteCarloPi` should only get arguments and calculate result - it should not draw it and not test it. Frankly, to calculate `monteCarloPi` you don't need `turtle`. – furas Sep 24 '21 at 17:40

1 Answers1

0

I see a number of potential problems with your code. The last two lines shown aren't indented correctly, they should be after the loop, not in it.

This equation origin = x ** 2 + y ** 2 seems incorrect and might need to instead be origin = (x ** 2 + y ** 2) ** 0.5

These two statements:

x = random.uniform(-radius, radius)
y = random.uniform(-radius, radius)

need to be at the top of the loop, not before the loop, otherwise you're plotting the same point n times, instead of n points. This calculation seems wrong:

pi = 4 * (points_in_circle / points_in_square)

reread the instructions you provided above. You should be dividing by n not points_in_square. This doesn't seem correct:

if origin <=1 :

as we're not working with a unit circle, so I'd expect:

if origin <= radius:

The reworked code based on my above observations:

from turtle import Screen, Turtle
from random import uniform

def monteCarloPi(n, radius):
    '''
    Takes two arguments, the number of simulations n and the radius of the circle,
    and returns a float, which is the estimated value for pi.

    '''

    turtle.dot()  # Origin (0, 0)

    turtle.penup()
    turtle.sety(-radius)
    turtle.pendown()
    turtle.circle(radius)

    turtle.penup()
    turtle.setx(-radius)
    turtle.pendown()

    for _ in range(4):
        turtle.forward(radius * 2)
        turtle.left(90)

    turtle.penup()
    turtle.hideturtle()

    points_in_circle = 0

    for _ in range(n):
        x = uniform(-radius, radius)
        y = uniform(-radius, radius)

        turtle.goto(x, y)
        turtle.dot()

        origin = (x ** 2 + y ** 2) ** 0.5

        if origin <= radius:
            points_in_circle += 1

    pi = 4 * points_in_circle / n

    return pi

screen = Screen()

turtle = Turtle()
turtle.speed('fastest')  # because I have no patience

print(monteCarloPi(100, 100))

screen.exitonclick()
cdlane
  • 40,441
  • 5
  • 32
  • 81