Skip to content

Commit c49c17f

Browse files
authored
Update pi_monte_carlo_estimation.py
1 parent c8924f4 commit c49c17f

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

maths/pi_monte_carlo_estimation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def random_unit_square(cls, ran: random.Random):
2626

2727
def estimate_pi(number_of_simulations: int, seed: int | None = None) -> float:
2828
"""
29-
Generates an estimate of the mathematical constant PI.
29+
Generate an estimate of the mathematical constant PI.
3030
See https://en.wikipedia.org/wiki/Monte_Carlo_method#Overview
3131
3232
The estimate is generated by Monte Carlo simulations. Let U be uniformly drawn from
@@ -46,7 +46,7 @@ def estimate_pi(number_of_simulations: int, seed: int | None = None) -> float:
4646
circle, which is called m.
4747
3. An estimate of P[U in unit circle] is m/n
4848
49-
'seed' provides seed for the number generator - if None, no seed is used.
49+
'seed' provides a seed for the number generator; if None, no seed is used.
5050
5151
>>> estimate_pi(100, 1)
5252
3.2
@@ -74,3 +74,6 @@ def estimate_pi(number_of_simulations: int, seed: int | None = None) -> float:
7474
import doctest
7575

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

0 commit comments

Comments
 (0)