Facebook
From Can Yilmaz, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 179
  1. import random
  2.  
  3. INTERVAL= 1000
  4.  
  5. circle_points= 0
  6. square_points= 0
  7.  
  8. # Total Random numbers generated= possible x
  9. # values* possible y values
  10. for i in range(INTERVAL**2):
  11.  
  12.         # Randomly generated x and y values from a
  13.         # uniform distribution
  14.         # Rannge of x and y values is -1 to 1
  15.         rand_x= random.uniform(-1, 1)
  16.         rand_y= random.uniform(-1, 1)
  17.  
  18.         # Distance between (x, y) from the origin
  19.         origin_dist= rand_x**2 + rand_y**2
  20.  
  21.         # Checking if (x, y) lies inside the circle
  22.         if origin_dist<= 1:
  23.                 circle_points+= 1
  24.  
  25.         square_points+= 1
  26.  
  27.         # Estimating value of pi,
  28.         # pi= 4*(no. of points generated inside the
  29.         # circle)/ (no. of points generated inside the square)
  30.         pi = 4* circle_points/ square_points
  31.  
  32. ## print(rand_x, rand_y, circle_points, square_points, "-", pi)
  33. ## print("\n")
  34.  
  35. print("Final Estimation of Pi=", pi)     
  36.  

Replies to estimating pi rss

Title Name Language When
Re: estimating pi Colossal Mockingbird python 3 Years ago.