fork download
  1. import random
  2. import math
  3.  
  4. N_tot = 0
  5. N_hits = 0
  6. centerX = 0.5
  7. centerY = 0.5
  8.  
  9. # precision = input('input number of decimals for approximation: ')
  10.  
  11.  
  12. def inside_circle(randomX,randomY):
  13. if(math.sqrt((centerX-randomX)**2+(centerY-randomY)**2)<=0.5):
  14. return True
  15. else:
  16. return False
  17.  
  18. def estimate_pi():
  19. N_tot = 0
  20. N_hits = 0
  21.  
  22. for i in range(0,100000):
  23. if(inside_circle(random.random(),random.random())==True):
  24. N_hits = N_hits+1
  25. N_tot = N_tot+1
  26. else:
  27. N_tot = N_tot+1
  28.  
  29. result = N_hits/N_tot
  30. return result
  31.  
  32. print(estimate_pi())
Success #stdin #stdout 0.08s 37616KB
stdin
Standard input is empty
stdout
0.78531