Advertisement
FelipeNeto2

Estimate the number PI

Oct 14th, 2022
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. #Estimate the number PI
  2. import random
  3.  
  4. def estimate_pi(n):
  5.     num_point_circle = 0
  6.     num_point_total = 0
  7.     for _ in range(n):
  8.         x = random.uniform(0,1)
  9.         y = random.uniform(0,1)
  10.         distance = x**2 + y**2
  11.         if distance <= 1:
  12.             num_point_circle += 1
  13.         num_point_total += 1
  14.     return 4 * num_point_circle/num_point_total    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement