true if the point lies in the unit circle false otherwise generates a point randomly drawn from the unit square 0 1 x 0 1 generates an estimate of the mathematical constant pi see https en wikipedia orgwikimontecarlomethodoverview the estimate is generated by monte carlo simulations let u be uniformly drawn from the unit square 0 1 x 0 1 the probability that u lies in the unit circle is pu in unit circle 14 pi and therefore pi 4 pu in unit circle we can get an estimate of the probability pu in unit circle see https en wikipedia orgwikiempiricalprobability by 1 draw a point uniformly from the unit square 2 repeat the first step n times and count the number of points in the unit circle which is called m 3 an estimate of pu in unit circle is mn import doctest doctest testmod true if the point lies in the unit circle false otherwise generates a point randomly drawn from the unit square 0 1 x 0 1 generates an estimate of the mathematical constant pi see https en wikipedia org wiki monte_carlo_method overview the estimate is generated by monte carlo simulations let u be uniformly drawn from the unit square 0 1 x 0 1 the probability that u lies in the unit circle is p u in unit circle 1 4 pi and therefore pi 4 p u in unit circle we can get an estimate of the probability p u in unit circle see https en wikipedia org wiki empirical_probability by 1 draw a point uniformly from the unit square 2 repeat the first step n times and count the number of points in the unit circle which is called m 3 an estimate of p u in unit circle is m n import doctest doctest testmod
import random


class Point:
    def __init__(self, x: float, y: float) -> None:
        self.x = x
        self.y = y

    def is_in_unit_circle(self) -> bool:
        
        return (self.x**2 + self.y**2) <= 1

    @classmethod
    def random_unit_square(cls):
        
        return cls(x=random.random(), y=random.random())


def estimate_pi(number_of_simulations: int) -> float:
    
    if number_of_simulations < 1:
        raise ValueError("At least one simulation is necessary to estimate PI.")

    number_in_unit_circle = 0
    for _ in range(number_of_simulations):
        random_point = Point.random_unit_square()

        if random_point.is_in_unit_circle():
            number_in_unit_circle += 1

    return 4 * number_in_unit_circle / number_of_simulations


if __name__ == "__main__":
    

    
    from math import pi

    prompt = "Please enter the desired number of Monte Carlo simulations: "
    my_pi = estimate_pi(int(input(prompt).strip()))
    print(f"An estimate of PI is {my_pi} with an error of {abs(my_pi - pi)}")