Abhisek92

MonteCarlo_pi.py

Feb 16th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. import random
  2. import math
  3. import os
  4.  
  5. total_point = 0
  6. in_circle = 0
  7. while total_point < 1000000:
  8.     x = int.from_bytes(os.urandom(8), byteorder="big") / ((1 << 64) - 1)
  9.     y = int.from_bytes(os.urandom(8), byteorder="big") / ((1 << 64) - 1)
  10.     total_point = total_point + 1
  11.     if (x**2)+(y**2) <= 1:
  12.         in_circle = in_circle + 1
  13. estimated_pi = ((4*in_circle)/total_point)
  14. print("Estimated pi: ", estimated_pi, "\tTrue pi: ", math.pi)
  15. print("Error: ", (math.pi - estimated_pi))
Add Comment
Please, Sign In to add comment