# Pi

Monte Carlo simulations are a class of computational algorithms that use repeated random sampling to obtain numerical results. In the context of estimating Pi ($$�π$$), a common approach is to simulate random points inside a square and count how many fall inside a quarter circle inscribed within that square. The ratio of points inside the quarter circle to the total number of points, multiplied by 4, gives an approximation of $$�π$$. This method is based on the principle that the area of the circle is $$pi\*r^2$$ and the area of the square is $$4r^2$$ (for a circle of radius $$�r$$).

````python
import random

# def estimate_pi(num_samples):
#     inside_circle = 0

#     for _ in range(num_samples):
#         x, y = random.uniform(-1, 1), random.uniform(-1, 1)
#         if x**2 + y**2 <= 1:
#             inside_circle += 1

#     pi_estimate = 4 * inside_circle / num_samples
#     return pi_estimate

# # Estimate Pi using 1,000,000 samples
# pi_estimate = estimate_pi(1000000)
# print(f"Estimated Pi: {pi_estimate}")

def estimate_pi(num_samples):
    inside_circle = 0

    for _ in range(num_samples):
        x, y = random.random(), random.random()  # Random point in [0,1] x [0,1]
        distance = x**2 + y**2  # Distance from (0,0)
        if distance <= 1:  # Check if inside the quarter circle
            inside_circle += 1

    return 4 * inside_circle / num_samples  # Approximation of Pi

# Example with 1,000,000 samples
pi_estimate = estimate_pi(1000000)
print(f"Estimated Pi: {pi_estimate}")```
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dongzeli95s-organization.gitbook.io/swe-interview-handbook/company-tags/citadel/pi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
