Advertisement
Guest User

Untitled

a guest
May 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. import math
  2. import random
  3.  
  4.  
  5. def euclidean_distance(x, y):
  6. return math.sqrt(x*x + y*y)
  7.  
  8. def montecarlo_simulation_for_calulcating_pii(num_iterations=100):
  9.  
  10. desired_count = 0
  11.  
  12. for i in range(0, num_iterations):
  13. x_coordinate = random.random()
  14. y_coordinate = random.random()
  15.  
  16. if euclidean_distance(x_coordinate, y_coordinate) <= 1:
  17. desired_count += 1
  18.  
  19.  
  20. return float(desired_count) * 4/ float(num_iterations)
  21.  
  22.  
  23. if __name__ == '__main__':
  24. print(montecarlo_simulation_for_calulcating_pii(10000))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement